################################################################################
## This R script reproduces the results from the paper "Bureaucratic Capacity ##
## and Political Favoritism in Public Procurement"                            ##
##                                                                            ##
## R Version: 4.3.1                                                           ##
## Operating system: macOS Sonoma 14.3                                        ##
##                                                                            ##
## Author: Diego Romero                                                       ##
## Institution: Utah State University                                         ##
################################################################################

rm(list = ls())
set.seed(1992)
options(scipen = 100)

library(tidyverse) # Version: 2.0.0
library(ggplot2) # Version: 3.4.2

# For creating tables:
library(stargazer) # Version: 5.2.3
library(modelsummary) # Version: 1.4.3

# feols
library(fixest) # Version: 0.11.2

# for implementing the binning estimator
library(interflex) # Version: 1.2.6

# for robust standard error estimation
library(lmtest) # Version: 0.9-40

# To calculate correct vcov matrix with 2WFE
library(sandwich) # Version: 3.0-2


# Set Working Directory
# setwd("")

# Import bid-level municipal procurement dataset 
df <- read.csv("gtm_bidleveldata_2013-2019.csv", stringsAsFactors = FALSE)
ls(df)
###########################################
##  Tables and Figures in the Main Text  ## 
###########################################

# Figure 1: Procurement Outcomes for Connected and Unconnected Firms

## Panel (A) Contracts to Connected Firms
## Preparing subset 
df_pa <- df[which(df$award==1),] %>%
  group_by(year,connectedbin) %>%
  summarise(value = n()) 

## Stacked Bar Plot: number of contracts per year per connection status of the contractor
ggplot(df_pa, aes(fill=as.factor(connectedbin), y=value, x=year)) + 
  geom_bar(position="stack", stat="identity") + 
  scale_fill_manual(values=c("darkolivegreen2", "darkolivegreen4"), name = "Connected", labels = c("No", "Yes")) + 
  xlab("Year") + ylab("Number of Contracts") +
  theme_classic()


## Panel (B) Bids from Connected Firms
## Preparing subset 
df_pb <- df %>%
  group_by(year,connectedbin) %>%
  summarise(value = n()) 

# Stacked Bar Plot with Colors and Legend
ggplot(df_pb, aes(fill=as.factor(connectedbin), y=value, x=year)) + 
  geom_bar(position="stack", stat="identity") +
  scale_fill_manual(values=c("darkolivegreen2", "darkolivegreen4"), name = "Connected", labels = c("No", "Yes")) + 
  xlab("Year") + ylab("Number of Bids") +
  theme_classic()


## Panel (C) Contracts to Connected Firms
## Preparing subset 
df_pc <- df[which(df$risky_award==1),] %>%
  group_by(year,connectedbin) %>%
  summarise(value = n()) 

## Stacked Bar Plot: number of contracts per year per connection status of the contractor
ggplot(df_pc, aes(fill=as.factor(connectedbin), y=value, x=year)) + 
  geom_bar(position="stack", stat="identity") + 
  scale_fill_manual(values=c("darkolivegreen2", "darkolivegreen4"), name = "Connected", labels = c("No", "Yes")) + 
  xlab("Year") + ylab("Number of Contracts to Sole Bidders") +
  theme_classic()


## Panel (D) Number of Bids per Purchase
## Preparing subset 
df_pd <- df[which(df$award==1),] %>%
  group_by(year,connectedbin) %>%
  summarise(value = mean(num_bids)) 

ggplot(data=df_pd, aes(x=year, y=value, colour=as.factor(connectedbin))) +
  scale_y_continuous(name="Number of Bids per Purchase") +
  scale_colour_manual(values=c("darkolivegreen2", "darkolivegreen4"), name = "Connected", labels = c("No", "Yes")) + 
  xlab("Year") + ylab("Number of Contracts to Sole Bidders") +
  geom_line() + theme_classic()


# Figure 2: Political Connections, Bureaucrats' Capabilities and Procurement Outcomes

## Marginal effect of a firm's connection on the probability of obtaining a procurement contract
## Estimators: linear and binning
award_plot_binning <- interflex(Y = "award", D = "connectedbin", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df[ which(df$b_capacity<=0.6), ], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE, wald=TRUE)
plot(award_plot_binning,theme.bw = TRUE)

print(award_plot_binning$tests$p.wald)


# Table 2: Political Connections, Bureaucrats' Capabilities and Procurement Awards

## Zero: Full model BUT without interaction
model0 <- lm(award ~ b_capacity + connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[ which(df$b_capacity<=0.6), ])
model0$vcov <- vcovCL(model0, cluster = ~ entity_code + year + nit_bidder)
model0_coef <- coeftest(model0, model0$vcov)
model0_cse <- sqrt(diag(model0$vcov))

## First: Model without controls and without FEs
model1 <- lm(award ~ b_capacity + connectedbin + b_capacity:connectedbin, data = df[ which(df$b_capacity<=0.6), ])
model1$vcov <- vcovCL(model1, cluster = ~ entity_code + year + nit_bidder)
model1_coef <- coeftest(model1, model1$vcov)
model1_cse <- sqrt(diag(model1$vcov))

## Second: Model with controls but no FEs
model2 <- lm(award ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president, data = df[ which(df$b_capacity<=0.6), ])
model2$vcov <- vcovCL(model2, cluster = ~ entity_code + year + nit_bidder)
model2_coef <- coeftest(model2, model2$vcov)
model2_cse <- sqrt(diag(model2$vcov))

## Third: Model with controls and FEs for Year and Municipality
model3 <- lm(award ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[ which(df$b_capacity<=0.6), ])
model3$vcov <- vcovCL(model3, cluster = ~ entity_code + year + nit_bidder)
model3_coef <- coeftest(model3, model3$vcov)
model3_cse <- sqrt(diag(model3$vcov))


# TABLE with clustered standard errors:
stargazer(model0, model1, model2, model3, se = list(model0_cse, model1_cse, model2_cse, model3_cse) , title="Connections, Bureaucrats' Capabilities and Procurement Awards",
          align=TRUE, dep.var.labels=c("award"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"),
          no.space=TRUE, add.lines = list(c("Controls", "Y"," ","Y","Y"),
                                          c("Municipality FE", "Y"," "," ","Y"),
                                          c("Year FE","Y"," "," ","Y")),  keep = c("Constant",  "b_capacity",  "connectedbin"))



# Figure 3: Shielding Connected Firms from Competition

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Panel A: Obtaining Contract as Single Bidder
raward_plot_binning <- interflex(Y = "risky_award", D = "connectedbin", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[ which(df_nog$b_capacity<=0.62), ], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", wald = TRUE)
plot(raward_plot_binning,theme.bw = TRUE)
print(raward_plot_binning$tests$p.wald)

## Panel B: Limiting the Number of Bidders
nbids_plot_binning <- interflex(Y = "num_bids", D = "connectedbin", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[ which(df_nog$b_capacity<=0.62 & df_nog$num_bids<20), ], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bids", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbids_plot_binning,theme.bw = TRUE)
print(nbids_plot_binning$tests$p.wald)


# Table 3: Political Connections, Bureaucrats' Capabilities and Competition in Public Procurement

## Outcome 1: Award to Single Bidder

## Zero: Full model BUT without interaction
rmodel0 <- lm(risky_award ~ b_capacity + connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[ which(df_nog$b_capacity<=0.62), ])
rmodel0$vcov <- vcovCL(rmodel0, cluster = ~ entity_code + year + nit_bidder)
rmodel0_coef = coeftest(rmodel0, rmodel0$vcov)
rmodel0_cse <- sqrt(diag(rmodel0$vcov))

## First: Model without controls and without FEs
rmodel1 <- lm(risky_award ~ b_capacity + connectedbin + b_capacity:connectedbin, data = df_nog[ which(df_nog$b_capacity<=0.62), ])
rmodel1$vcov <- vcovCL(rmodel1, cluster = ~ entity_code + year + nit_bidder)
rmodel1_coef = coeftest(rmodel1, rmodel1$vcov)
rmodel1_cse <- sqrt(diag(rmodel1$vcov))

## Second: Model with controls but no FEs
rmodel2 <- lm(risky_award ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president, data = df_nog[ which(df_nog$b_capacity<=0.62), ])
rmodel2$vcov <- vcovCL(rmodel2, cluster = ~ entity_code + year + nit_bidder)
rmodel2_coef = coeftest(rmodel2, rmodel2$vcov)
rmodel2_cse <- sqrt(diag(rmodel2$vcov))

## Third: Model with controls and FEs for Year and Municipality
rmodel3 <- lm(risky_award ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[ which(df_nog$b_capacity<=0.62), ])
rmodel3$vcov <- vcovCL(rmodel3, cluster = ~ entity_code + year + nit_bidder)
rmodel3_coef = coeftest(rmodel3, rmodel3$vcov)
rmodel3_cse <- sqrt(diag(rmodel3$vcov))

## Outcome 2: Number of Competing Bids

## Fourth: Full model BUT without interaction
rmodel4 <- lm(num_bids ~ b_capacity + connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[ which(df_nog$b_capacity<=0.62 & df_nog$num_bids<20), ])
rmodel4$vcov <- vcovCL(rmodel4, cluster = ~ entity_code + year + nit_bidder)
rmodel4_coef = coeftest(rmodel4, rmodel4$vcov)
rmodel4_cse <- sqrt(diag(rmodel4$vcov))

## Fifth: Model without controls and without FEs
rmodel5 <- lm(num_bids ~ b_capacity + connectedbin + b_capacity:connectedbin, data = df_nog[ which(df_nog$b_capacity<=0.62 & df_nog$num_bids<20), ])
rmodel5$vcov <- vcovCL(rmodel5, cluster = ~ entity_code + year + nit_bidder)
rmodel5_coef = coeftest(rmodel5, rmodel5$vcov)
rmodel5_cse <- sqrt(diag(rmodel5$vcov))

## Sixth: Model with controls but no FEs
rmodel6 <- lm(num_bids~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president, data = df_nog[ which(df_nog$b_capacity<=0.62 & df_nog$num_bids<20), ])
rmodel6$vcov <- vcovCL(rmodel6, cluster = ~ entity_code + year + nit_bidder)
rmodel6_coef = coeftest(rmodel6, rmodel6$vcov)
rmodel6_cse <- sqrt(diag(rmodel6$vcov))

## Seventh: Model with controls and FEs for Year and Municipality
rmodel7 <- lm(num_bids ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[ which(df_nog$b_capacity<=0.62 & df_nog$num_bids<20), ])
rmodel7$vcov <- vcovCL(rmodel7, cluster = ~ entity_code + year + nit_bidder)
rmodel7_coef = coeftest(rmodel7, rmodel7$vcov)
rmodel7_cse <- sqrt(diag(rmodel7$vcov))

# TABLE with clustered standard errors:
stargazer(rmodel0, rmodel1, rmodel2, rmodel3, rmodel4, rmodel5, rmodel6, rmodel7, 
          se = list(rmodel0_cse, rmodel1_cse, rmodel2_cse, rmodel3_cse, rmodel4_cse, rmodel5_cse, rmodel6_cse, rmodel7_cse),
          title="Political Connections, Bureaucrats' Capabilities and Competition in Public Procurement",
          align=TRUE, star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"),
          no.space=TRUE, add.lines = list(c("Controls", "Y"," ","Y","Y", "Y"," ","Y","Y"),
                                          c("Municipality FE", "Y"," "," ","Y", "Y"," "," ","Y"),
                                          c("Year FE","Y"," "," ","Y","Y"," "," ","Y")),  keep = c("Constant",  "b_capacity",  "connectedbin"))





# Figure 4: Bunching Below the Threshold for Increased Scrutiny

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Bunching around the Q90,000 mark (keeping contracts between Q40,000 and Q140,000)
df_p1 <- df_nog[which(df_nog$contract_value>40000 & df_nog$contract_value<140000) ,]

## Binary variable: high (above the mean) bureaucratic capabilities vs low
df_p1 <- df_p1 %>%
  mutate(high_bci = case_when(year < 2016 & b_capacity > 0.2081 ~ "High",
                              year < 2016 & b_capacity <= 0.2081 ~ "Low",
                              year >=2016 & b_capacity > 0.3901 ~ "High",
                              year >=2016 & b_capacity <= 0.3901 ~ "Low"))

## Panel A: All contracts near the threshold
ggplot(df_p1, aes(x=contract_value,fill=high_bci)) + 
  labs(fill = "BCI") +
  scale_fill_discrete(labels=c('High','Low')) +
  geom_histogram(aes(y = after_stat(count / sum(count))),binwidth=2000) + 
  scale_y_continuous(labels = scales::percent) +
  geom_vline(aes(xintercept=90000),color="black", linetype="solid", size=0.5) +
  theme_bw() + 
  xlab("Contract Value") +
  ylab("Percent") + 
  labs(title = "Contracts Around the Q90,000 Threshold")

df_p1 <- df_p1 %>%
  mutate(connected = case_when(connectedbin == 1 ~ "Yes",
                               connectedbin == 0 ~ "No"))

## Panel B: Contracts won by connected firms (in municipalities with high BCI)
ggplot(df_p1[which(df_p1$high_bci=="High"),], aes(x=contract_value,fill=connected)) + 
  labs(fill = "Connection") +
  scale_fill_discrete(labels=c('Yes','No')) +
  geom_histogram(aes(y = after_stat(count / sum(count))),binwidth=2000) + 
  scale_y_continuous(labels = scales::percent) +
  geom_vline(aes(xintercept=90000),color="black", linetype="solid", size=0.5) +
  theme_bw() + 
  xlab("Contract Value") +
  ylab("Percent") + 
  labs(title = "Contracts Around the Q90,000 Threshold (in High BCI Municipalities)")


# Table 4: Political Connections, Bureaucrats' Capabilities and Shielding Strategies 

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Linear Probability Models 

## Winning as the last bidder (only defined for electronic submissions)
model_winlast <- lm(connectedbin ~ b_capacity + win_last + b_capacity:win_last + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog)
model_winlast$vcov <- vcovCL(model_winlast, cluster = ~ entity_code + year + nit_bidder)
model_winlast_coef <- coeftest(model_winlast, model_winlast$vcov)
model_winlast_cse <- sqrt(diag(model_winlast$vcov))

## Window to present bids
model_biddingw <- lm(connectedbin ~ b_capacity + tiempo_publicacion_cierre + b_capacity:tiempo_publicacion_cierre + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog)
model_biddingw$vcov <- vcovCL(model_biddingw, cluster = ~ entity_code + year + nit_bidder)
model_biddingw_coef <- coeftest(model_biddingw, model_biddingw$vcov)
model_biddingw_cse <- sqrt(diag(model_biddingw$vcov))

## Window to adjudicate contracts
model_awardw <- lm(connectedbin ~ b_capacity + tiempo_cierre_adjudicacion + b_capacity:tiempo_cierre_adjudicacion + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog)
model_awardw$vcov <- vcovCL(model_awardw, cluster = ~ entity_code + year + nit_bidder)
model_awardw_coef <- coeftest(model_awardw, model_awardw$vcov)
model_awardw_cse <- sqrt(diag(model_awardw$vcov))

## Ratio: objective criteria to documents
df_nog$objective_doc_ratio <- NA
df_nog$objective_doc_ratio <- (df_nog$crt_object+0.01)/(df_nog$crt_docume+0.01)
df_nog$ln_objective_doc_ratio <- log(df_nog$objective_doc_ratio)

## Objective attributes to paperwork ratio:
model_objdoc_p <- lm(connectedbin ~ b_capacity + ln_objective_doc_ratio + b_capacity:ln_objective_doc_ratio + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog)
model_objdoc_p$vcov <- vcovCL(model_objdoc_p, cluster = ~ entity_code + year + nit_bidder)
model_objdoc_p_coef <- coeftest(model_objdoc_p, model_objdoc_p$vcov)
model_objdoc_p_cse <- sqrt(diag(model_objdoc_p$vcov))

## Ratio: subjective aspects of the firm to documents
df_nog$firm_doc_ratio <- NA
df_nog$firm_doc_ratio <- (df_nog$crt_subfir_excp+0.01)/(df_nog$crt_docume+0.01)
df_nog$ln_firm_doc_ratio <- log(df_nog$firm_doc_ratio)

## Firm attributes to to paperwork ratio:
model_firmdoc_p <- lm(connectedbin ~ b_capacity + ln_firm_doc_ratio + b_capacity:ln_firm_doc_ratio + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog)
model_firmdoc_p$vcov <- vcovCL(model_firmdoc_p, cluster = ~ entity_code + year + nit_bidder)
model_firmdoc_p_coef <- coeftest(model_firmdoc_p, model_firmdoc_p$vcov)
model_firmdoc_p_cse <- sqrt(diag(model_firmdoc_p$vcov))

## TABLE
stargazer(model_winlast, model_biddingw, model_awardw, model_objdoc_p, model_firmdoc_p,    
          se=list(model_winlast_cse, model_biddingw_cse, model_awardw_cse, model_objdoc_p_cse, model_firmdoc_p_cse),
          title="Political Connections, Bureucrats' Capabilities and Shielding Strategies",
          align=TRUE, dep.var.labels=c("Contract with Connected Firm"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"), 
          no.space=TRUE, keep = c("Constant","b_capacity","win_last","tiempo_publicacion_cierre","tiempo_cierre_adjudicacion", "ln_objective_doc_ratio","ln_firm_doc_ratio"))




###########################################
##  Tables and Figures in Appendix A     ## 
###########################################

# Table A1: Descriptive Statistics (Sample of All Bids)

df_ds1 = subset(df, select = c(monto_ofertado, n_winners, electronic, logvalcontracts, percontracts_muninei, percontracts_munisam, numcontracts_gov, firm_procage, construction, licitacion, cotizacion, directpurchase, other, rent_usdk, connectedbin, b_capacity, award, risky_award, num_bids, win_last, tiempo_publicacion_cierre, tiempo_cierre_adjudicacion,ln_objective_doc_ratio, ln_firm_doc_ratio, inhab_before, inhab_anytime))
stargazer(df_ds1)

# Table A2: Descriptive Statistics (Sample of All Contracts)
## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

df_ds2 = subset(df_nog, select = c(contract_value, n_winners, electronic, logvalcontracts, percontracts_muninei, percontracts_munisam, numcontracts_gov, firm_procage, construction, licitacion, cotizacion, directpurchase, other, rent_usdk, connectedbin, b_capacity, risky_award, num_bids, win_last, tiempo_publicacion_cierre, tiempo_cierre_adjudicacion,ln_objective_doc_ratio, ln_firm_doc_ratio, inhab_before, inhab_anytime))
stargazer(df_ds2)


# Figure A1: Linear Interaction Diagnostics Plots - Main Results

## Panel A: Award
award_raw <- interflex(estimator = "raw", Y = "award", D = "connectedbin", X = "b_capacity", ncols=2, data = df, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(award_raw,theme.bw = TRUE)

## Panel B: Award as Single Bidder
df_nog <- df[ which(df$award==1), ]

raward_raw <- interflex(estimator = "raw", Y = "risky_award", D = "connectedbin", X = "b_capacity", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(raward_raw,theme.bw = TRUE)

## Panel C: Number of Bids
df_nog <- df[ which(df$award==1), ]

nbids_raw <- interflex(estimator = "raw", Y = "num_bids", D = "connectedbin", X = "b_capacity", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bids", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbids_raw,theme.bw = TRUE)


# Figure A2: Political Connections, Bureaucrats’ Capabilities, Procurement Awards and Competition (Kernel Estimator)

## Panel A: Award
award_plot_kernel <- interflex(Y = "award", D = "connectedbin", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df[ which(df$b_capacity<=0.6), ], estimator = "kernel", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(award_plot_kernel)

## Panel B: Award as Single Bidder
df_nog <- df[ which(df$award==1), ]

raward_plot_kernel <- interflex(Y = "risky_award", D = "connectedbin", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[ which(df_nog$b_capacity<=0.62), ], estimator = "kernel", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(raward_plot_kernel)

## Panel C: Number of Bids
df_nog <- df[ which(df$award==1), ]

nbids_plot_kernel <- interflex(Y = "num_bids", D = "connectedbin", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[ which(df_nog$b_capacity<=0.62 & df_nog$num_bids<20), ], estimator = "kernel", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bids", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbids_plot_kernel)



# Table A3: Political Connections, Bureaucrats’ Capabilities and Procurement Awards (Logistic Regressions)

## Zero: Full model BUT without interaction
model0_lgt = glm(award ~ b_capacity + connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[ which(df$b_capacity<=0.6), ], family = binomial)
model0_lgt$vcov <- vcovCL(model0_lgt, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
model0_lgt_coef = coeftest(model0_lgt, model0_lgt$vcov)
model0_lgt_cse <- sqrt(diag(model0_lgt$vcov))

## First: Model without controls and without FEs
model1_lgt <- glm(award ~ b_capacity + connectedbin + b_capacity:connectedbin, data = df[ which(df$b_capacity<=0.6), ], family = binomial)
model1_lgt$vcov <- vcovCL(model1_lgt, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
model1_lgt_coef = coeftest(model1_lgt, model1_lgt$vcov)
model1_lgt_cse <- sqrt(diag(model1_lgt$vcov))

## Second: Model with controls but no FEs
model2_lgt <- glm(award ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president, data = df[ which(df$b_capacity<=0.6), ], family = binomial)
model2_lgt$vcov <- vcovCL(model2_lgt, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
model2_lgt_coef = coeftest(model2_lgt, model2_lgt$vcov)
model2_lgt_cse <- sqrt(diag(model2_lgt$vcov))

## Third: Model with controls and FEs for Year and Municipality
model3_lgt <- glm(award ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[ which(df$b_capacity<=0.6), ], family = binomial)
model3_lgt$vcov <- vcovCL(model3_lgt, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
model3_lgt_coef = coeftest(model3_lgt, model3_lgt$vcov)
model3_lgt_cse <- sqrt(diag(model3_lgt$vcov))

# TABLE with clustered standard errors:
stargazer(model0_lgt, model1_lgt, model2_lgt, model3_lgt, se = list(model0_lgt_cse, model1_lgt_cse, model2_lgt_cse, model3_lgt_cse), title="Political Connections, Bureaucrats' Capabilities and Awards (Logistic Regression)",
          align=TRUE, dep.var.labels=c("award"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"),
          no.space=TRUE, add.lines = list(c("Controls", "Y"," ","Y","Y"),
                                          c("Municipality FE", "Y"," "," ","Y"),
                                          c("Year FE","Y"," "," ","Y")),  keep = c("Constant",  "b_capacity",  "connectedbin"))


# Table A4: Political Connections, Bureaucrats’ Capabilities and Awards to Single Bidders (Logistic Regression)

## Zero: Full model BUT without interaction
rmodel0_lgt = glm(risky_award ~ b_capacity + connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[ which(df_nog$b_capacity<=0.62), ], family = binomial)
rmodel0_lgt$vcov <- vcovCL(rmodel0_lgt, cluster = ~ entity_code + year, type = "HC0")
rmodel0_lgt_coef = coeftest(rmodel0_lgt, rmodel0_lgt$vcov)
rmodel0_lgt_cse <- sqrt(diag(rmodel0_lgt$vcov))

## First: Model without controls and without FEs
rmodel1_lgt <- glm(risky_award ~ b_capacity + connectedbin + b_capacity:connectedbin, data = df_nog[ which(df_nog$b_capacity<=0.62), ], family = binomial)
rmodel1_lgt$vcov <- vcovCL(rmodel1_lgt, cluster = ~ entity_code + year, type = "HC0")
rmodel1_lgt_coef = coeftest(rmodel1_lgt, rmodel1_lgt$vcov)
rmodel1_lgt_cse <- sqrt(diag(rmodel1_lgt$vcov))

## Second: Model with controls but no FEs
rmodel2_lgt <- glm(risky_award ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president, data = df_nog[ which(df_nog$b_capacity<=0.62), ], family = binomial)
rmodel2_lgt$vcov <- vcovCL(rmodel2_lgt, cluster = ~ entity_code + year, type = "HC0")
rmodel2_lgt_coef = coeftest(rmodel2_lgt, rmodel2_lgt$vcov)
rmodel2_lgt_cse <- sqrt(diag(rmodel2_lgt$vcov))

## Third: Model with controls and FEs for Year and Municipality
rmodel3_lgt <- glm(risky_award ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[ which(df_nog$b_capacity<=0.62), ], family = binomial)
rmodel3_lgt$vcov <- vcovCL(rmodel3_lgt, cluster = ~ entity_code + year, type = "HC0")
rmodel3_lgt_coef = coeftest(rmodel3_lgt, rmodel3_lgt$vcov)
rmodel3_lgt_cse <- sqrt(diag(rmodel3_lgt$vcov))

# TABLE with clustered standard errors:
stargazer(rmodel0_lgt, rmodel1_lgt, rmodel2_lgt, rmodel3_lgt, 
          se = list(rmodel0_lgt_cse, rmodel1_lgt_cse, rmodel2_lgt_cse, rmodel3_lgt_cse),
          title="Connections, Bureaucrats' Capabilities and Awards to Single Bidders (Logistic Regression)",
          align=TRUE, dep.var.labels=c("Award as Single Bidder"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"),
          no.space=TRUE, add.lines = list(c("Controls", "Y"," ","Y","Y"),
                                          c("Municipality FE", "Y"," "," ","Y"),
                                          c("Year FE","Y"," "," ","Y")),  keep = c("Constant",  "b_capacity",  "connectedbin"))




# Table A5: Political Connections, Bureaucrats’ Capabilities, Awards and Competition (Firm FEs)

## First: Linear Probability Model with controls and FEs for Year and Municipality and Firm
model1_ffe <- feols(award ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president | factor(entity_code) + factor(year) + factor(nit_bidder), data = df[ which(df$b_capacity<=0.6), ], vcov = ~ entity_code + year + nit_bidder)

## First: Linear Probability Model with controls and FEs for Year and Municipality and Firm
df_nog <- df[ which(df$award==1), ]
rmodel1_ffe <- feols(risky_award ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president | factor(entity_code) + factor(year) + factor(nit_bidder), data = df_nog[ which(df_nog$b_capacity<=0.62), ], vcov = ~ entity_code + year + nit_bidder)

## Second: Linear Regression Model with controls and FEs for Year and Municipality and Firm
df_nog <- df[ which(df$award==1), ]
rmodel3_ffe <- feols(num_bids ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president | factor(entity_code) + factor(year) + factor(nit_bidder), data = df_nog[ which(df_nog$b_capacity<=0.62 & df_nog$num_bids<20), ], vcov = ~ entity_code + year + nit_bidder)

## Table:
modelresults <- list("Award" = model1_ffe, "Award to Single Bidder" = rmodel1_ffe, "No. of Bids" = rmodel3_ffe) 

rows <- tribble(~Controls,~Y,~Y,~Y,
                "Municipality FE","Y","Y","Y",
                "Year FE","Y","Y","Y",
                "Firm FE","Y","Y","Y")

modelsummary(modelresults, stars_note = TRUE, stars = TRUE, 
             coef_map = c('b_capacity' = 'Capabilities Index', 'connectedbin' = 'Connection', 'b_capacity:connectedbin' = 'Capabilities Index X Connection'), add_rows = rows, notes = 'Standard errors are clustered at the Municipality, Year and Firm levels.',  output = 'latex') 



# Figure A3: Linear Interaction Diagnostics Plots - (results excluding firms that operate exclusively in 1 municipality)

## Q. How many firms PRESENTED BIDS in only in one municipality?
df_firmsb <- df %>%
  group_by(nit_bidder,entity_code) %>%
  summarise_at(vars(connectedbin), 
               list(connectedbin = max)) 
df_firmsb <- df_firmsb %>% dplyr::count(nit_bidder) 
nrow(df_firmsb[df_firmsb$n==1,])/nrow(df_firmsb)

## Left join - bids
df_nolocalfirms <- merge(x=df,y=df_firmsb, by="nit_bidder", all.x=TRUE)
df_nolocalfirms <- df_nolocalfirms[df_nolocalfirms$n>1,]

## Panel A: Award
award_raw_n <- interflex(estimator = "raw", Y = "award", D = "connectedbin", X = "b_capacity", ncols=2, data = df_nolocalfirms, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(award_raw_n,theme.bw = TRUE)

## contracts
df_nolocalfirms_nog <- df_nolocalfirms[ which(df_nolocalfirms$award==1), ]

## Panel B: Award to Single Bidder
raward_raw_n <- interflex(estimator = "raw", Y = "risky_award", D = "connectedbin", X = "b_capacity", ncols=2, data = df_nolocalfirms_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(raward_raw_n,theme.bw = TRUE)

## Panel C: Number of Bids
nbids_raw_n <- interflex(estimator = "raw", Y = "num_bids", D = "connectedbin", X = "b_capacity", ncols=2, data = df_nolocalfirms_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bidders", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbids_raw_n,theme.bw = TRUE)



# Figure A4: Political Connections, Bureaucrats’ Capabilities, Procurement Awards and Competition (Binning Estimator, excluding firms that operate exclusively in 1 municipality)

## Sample of common support between connected and not.
dftn <- df_nolocalfirms[ which(df_nolocalfirms$b_capacity<=0.6), ]
dftn <- dftn[ which(dftn$b_capacity>=0.05), ]

## Panel A: Award 
award_plot_binning_n <- interflex(Y = "award", D = "connectedbin", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = dftn, estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE, wald=TRUE)
plot(award_plot_binning_n,theme.bw = TRUE)

## Sample of common support between connected and not.
dftn_nog <- df_nolocalfirms_nog[ which(df_nolocalfirms_nog$b_capacity<=0.6), ]
dftn_nog <- dftn_nog[ which(dftn_nog$b_capacity>=0.05), ]

## Panel B: Award to Single Bidder
raward_plot_binning_n <- interflex(Y = "risky_award", D = "connectedbin", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = dftn_nog, estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE, wald=TRUE)
plot(raward_plot_binning_n,theme.bw = TRUE)

## Panel C: Number of Bids
nbids_plot_binning_n <- interflex(Y = "num_bids", D = "connectedbin", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = dftn_nog[dftn_nog$num_bids<20,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bidders", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE, wald=TRUE)
plot(nbids_plot_binning_n,theme.bw = TRUE)



# Table A6: Political Connections, Bureaucrats’ Capabilities and Awards (Alternative Measures of Bureaucrats’ Capabilities)

## m1: Award, linear
m1 <- lm(award ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = dftn)
m1$vcov <- vcovCL(m1, cluster = ~ entity_code + year + nit_bidder)
m1_coef <- coeftest(m1, m1$vcov)
m1_cse <- sqrt(diag(m1$vcov))

## m2: Award, logit
m2 <- glm(award ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = dftn, family = "binomial")
m2$vcov <- vcovCL(m2, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
m2_coef <- coeftest(m2, m2$vcov)
m2_cse <- sqrt(diag(m2$vcov))

## m3: Award as Single Bidder, linear
m3 <- lm(risky_award ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = dftn_nog)
m3$vcov <- vcovCL(m3, cluster = ~ entity_code + year + nit_bidder)
m3_coef <- coeftest(m3, m3$vcov)
m3_cse <- sqrt(diag(m3$vcov))

## m4: Award as Single Bidder, logit
m4 <- glm(risky_award ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = dftn_nog, family = "binomial")
m4$vcov <- vcovCL(m4, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
m4_coef <- coeftest(m4, m4$vcov)
m4_cse <- sqrt(diag(m4$vcov))

## m5: Number of Competing Bids, liner
m5 <- lm(num_bids~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = dftn_nog[dftn_nog$num_bids<20,])
m5$vcov <- vcovCL(m5, cluster = ~ entity_code + year + nit_bidder)
m5_coef <- coeftest(m5, m5$vcov)
m5_cse <- sqrt(diag(m5$vcov))


# TABLE: results are robust to the exclusion of single-muncipality firms.
stargazer(m1, m2, m3, m4, m5, 
          se = list(m1_cse, m2_cse, m3_cse, m4_cse, m5_cse), 
          title="Political Connections, Bureaucrats' Capabilities, Procurement and Competition (dropping firms that participate in only one Municipality)",
          align=TRUE, dep.var.labels=c("award"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"),
          no.space=TRUE, add.lines = list(c("Controls", "Y","Y","Y","Y"),
                                          c("Municipality FE", "Y","Y","Y","Y"),
                                          c("Year FE","Y","Y","Y","Y")),  keep = c("Constant",  "b_capacity",  "connectedbin"))



# Table A7: Political Connections, Bureaucrats’ Capabilities and Share of Awards to Single Bidders

## Subset of contracts
df_nog <- df[ which(df$award==1), ]

## New variable: award as single bidder to connected firm
df_nog <- df_nog %>%
  mutate(award_sb_conn = case_when(connectedbin == 1 & risky_award == 1 ~ 1,
                                   connectedbin == 1 & risky_award == 0 ~ 0,
                                   connectedbin == 0 ~ 0))

## Municipal-level dataset
df_muni <- df_nog %>%
  group_by(entity_code,year) %>%
  summarise_at(vars(connectedbin,award,risky_award,award_sb_conn,b_capacity,b_capacity_alt,iga,ige,igf,dept_code,population,numcontracts,iic), 
               list(munilevel = mean,maxmuni = max)) 

df_muni <- subset(df_muni, select = c(entity_code,year,connectedbin_munilevel, award_munilevel,risky_award_munilevel, award_sb_conn_munilevel,b_capacity_maxmuni,b_capacity_alt_maxmuni,
                                      iga_munilevel,ige_munilevel,igf_munilevel,dept_code_maxmuni,population_maxmuni,numcontracts_maxmuni,iic_munilevel))

## Specification suggested by reviewer:
## \% of single bidding as a function of:
## bureaucratic capacity * proportion of firm-mayor connections

## Model 1x: Share of contracts to connected single bidders (linear model without controls)
m1x <- lm(risky_award_munilevel ~ b_capacity_maxmuni + connectedbin_munilevel + b_capacity_maxmuni:connectedbin_munilevel, data = df_muni)
m1x$vcov <- vcovCL(m1x, cluster = ~ entity_code)
m1x_coef <- coeftest(m1x, m1x$vcov)
m1x_coef
m1x_cse <- sqrt(diag(m1x$vcov))

## Model 4x: Share of contracts to connected single bidders (linear model with controls )
m2x <- lm(risky_award_munilevel ~ b_capacity_maxmuni + connectedbin_munilevel + b_capacity_maxmuni:connectedbin_munilevel + population_maxmuni + numcontracts_maxmuni + iic_munilevel, data = df_muni)
m2x$vcov <- vcovCL(m2x, cluster = ~ entity_code)
m2x_coef <- coeftest(m2x, m2x$vcov)
m2x_coef
m2x_cse <- sqrt(diag(m2x$vcov))

## Model 2x: Share of contracts to connected single bidders (linear model with controls and FEs for Year and Municipality)
m3x <- lm(risky_award_munilevel ~ b_capacity_maxmuni + connectedbin_munilevel + b_capacity_maxmuni:connectedbin_munilevel + population_maxmuni + numcontracts_maxmuni + iic_munilevel + factor(entity_code) + factor(year), data = df_muni)
m3x$vcov <- vcovCL(m3x, cluster = ~ entity_code)
m3x_coef <- coeftest(m3x, m3x$vcov)
m3x_cse <- sqrt(diag(m3x$vcov))
m3x_coef

## Model 3x: Share of contracts to connected single bidders (linear model with controls and FEs for Year and Departamento)
m4x <- lm(risky_award_munilevel ~ b_capacity_maxmuni + connectedbin_munilevel + b_capacity_maxmuni:connectedbin_munilevel + population_maxmuni + numcontracts_maxmuni + iic_munilevel + factor(dept_code_maxmuni) + factor(year), data = df_muni)
m4x$vcov <- vcovCL(m4x, cluster = ~ entity_code)
m4x_coef <- coeftest(m4x, m4x$vcov)
m4x_cse <- sqrt(diag(m4x$vcov))
m4x_coef

# TABLE with clustered standard errors:
stargazer(m1x, m2x, m3x, m4x, 
          #se = list(m1x_cse, m2x_cse, m3x_cse, m4x_cse,), 
          title="Political Connections, Bureaucrats' Capabilities and Share of Awards to Single Bidders",
          align=TRUE, 
          dep.var.labels=c("Share of Awards to Single Bidders")
          , star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"),
          no.space=TRUE, add.lines = list(c("Controls"," ", "Y","Y","Y"),
                                          c("Municipality FE"," "," ","Y"," "),
                                          c("Department FE", " "," "," ","Y"),
                                          c("Year FE"," ", " ","Y","Y")),  keep = c("Constant",  "b_capacity_maxmuni","connectedbin_munilevel"))




# Table A8: Political Connections, Bureaucrats’ Capabilities and Quality of Contractors

## Subset of contracts
df_nog <- df[ which(df$award==1), ]

# Disqualifications
df_nog$inhab_before_bin = NA
df_nog$inhab_before_bin[which(df_nog$inhab_before > 0 )] <- 1
df_nog$inhab_before_bin[which(df_nog$inhab_before == 0 )] <- 0

df_nog$inhab_after_bin = NA
df_nog$inhab_after_bin[which(df_nog$inhab_anytime > 0 )] <- 1
df_nog$inhab_after_bin[which(df_nog$inhab_anytime == 0 )] <- 0
df_nog$inhab_after_bin[which(df_nog$inhab_before > 0 )] <- 0

## Sample of common support between connected and not.
dft_nog <- df_nog[ which(df_nog$b_capacity<=0.62), ]

## Model 1: Disqualifications before, all awards (linear model with controls and FEs for Year and Municipality)
m1_all <- lm(inhab_before_bin ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_nog)
m1_all$vcov <- vcovCL(m1_all, cluster = ~ entity_code + year + nit_bidder)
m1_all_coef <- coeftest(m1_all, m1_all$vcov)
m1_all_cse <- sqrt(diag(m1_all$vcov))

## Model 2: Disqualifications before, all awards (logit model with controls and FEs for Year and Municipality)
m2_all <- glm(inhab_before_bin ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_nog, family = binomial)
m2_all$vcov <- vcovCL(m2_all, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
m2_all_coef <- coeftest(m2_all, m2_all$vcov)
m2_all_cse <- sqrt(diag(m2_all$vcov))

## Model 3: Disqualifications at any time, all awards (linear model with controls and FEs for Year and Municipality)
m3_all <- lm(inhab_after_bin ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_nog)
m3_all$vcov <- vcovCL(m3_all, cluster = ~ entity_code + year + nit_bidder)
m3_all_coef <- coeftest(m3_all, m3_all$vcov)
m3_all_cse <- sqrt(diag(m3_all$vcov))

## Model 4: Disqualifications at any time, all awards (logit model with controls and FEs for Year and Municipality)
m4_all <- glm(inhab_after_bin ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_nog, family = binomial)
m4_all$vcov <- vcovCL(m4_all, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
m4_all_coef <- coeftest(m4_all, m4_all$vcov)
m4_all_cse <- sqrt(diag(m4_all$vcov))


## Sample of awards to single bidders
df_nog_sb <- df_nog[ which(df_nog$risky_award==1), ]
dft_nog_sb <- dft_nog[ which(dft_nog$risky_award==1), ]

## Model 5: Disqualifications before, awards to single bidders (linear model with controls and FEs for Year and Municipality)
m5_all <- lm(inhab_before_bin ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_nog_sb)
m5_all$vcov <- vcovCL(m5_all, cluster = ~ entity_code + year + nit_bidder)
m5_all_coef <- coeftest(m5_all, m5_all$vcov)
m5_all_cse <- sqrt(diag(m5_all$vcov))

## Model 6: Disqualifications before, awards to single bidders (logit model with controls and FEs for Year and Municipality)
m6_all <- glm(inhab_before_bin ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_nog_sb, family = binomial)
m6_all$vcov <- vcovCL(m6_all, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
m6_all_coef <- coeftest(m6_all, m6_all$vcov)
m6_all_cse <- sqrt(diag(m6_all$vcov))

## Model 7: Disqualifications at any time, awards to single bidders (linear model with controls and FEs for Year and Municipality)
m7_all <- lm(inhab_after_bin ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_nog_sb)
m7_all$vcov <- vcovCL(m7_all, cluster = ~ entity_code + year + nit_bidder)
m7_all_coef <- coeftest(m7_all, m7_all$vcov)
m7_all_cse <- sqrt(diag(m7_all$vcov))

## Model 8: Disqualifications at any time, awards to single bidders (logit model with controls and FEs for Year and Municipality)
m8_all <- glm(inhab_after_bin ~ b_capacity + connectedbin + b_capacity:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_nog_sb, family = binomial)
m8_all$vcov <- vcovCL(m8_all, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
m8_all_coef <- coeftest(m8_all, m8_all$vcov)
m8_all_cse <- sqrt(diag(m8_all$vcov))


# TABLE with clustered standard errors:
stargazer(m1_all, m2_all, m3_all, m4_all, m5_all, m6_all, m7_all, m8_all, 
          se = list(m1_all_cse, m2_all_cse, m3_all_cse, m4_all_cse, m5_all_cse, m6_all_cse, m7_all_cse, m8_all_cse), 
          title="Political Connections, Bureaucrats' Capabilities and Quality of Contractors",
          align=TRUE, 
          dep.var.labels=c("Disqualifications (Before)","Disqualifications (After)")
          , star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"),
          no.space=TRUE, add.lines = list(c("Controls", "Y","Y","Y","Y","Y","Y","Y","Y"),
                                          c("Municipality FE", "Y","Y","Y","Y","Y","Y","Y","Y"),
                                          c("Year FE","Y","Y","Y","Y","Y","Y","Y","Y")),  keep = c("Constant",  "b_capacity",  "connectedbin"))



###########################################
##  Tables and Figures in Appendix B     ## 
###########################################

# Figure B1: Changes in the Bureaucrats’ Capabilities Index

## New variable defining period
df <- df %>%
  mutate(mayor_period = case_when(year >= 2016 ~ "2016-2019",
                                  year < 2016 ~ "2012-2015"))

## New dataframe: Municipality-period level
df_bci <- df %>%
  group_by(entity_code,mayor_period) %>%
  summarise_at(vars(b_capacity),list(b_capacity = max)) 
ls(df_bci)

## Subsets per period
df_sub_p1 <- df_bci[which(df_bci$mayor_period=="2012-2015"),]
summary(df_sub_p1$b_capacity)
df_sub_p2 <- df_bci[which(df_bci$mayor_period=="2016-2019"),]
summary(df_sub_p2$b_capacity)

df_bci2 <- df_sub_p1 %>% left_join(df_sub_p2, 
                                   by=c('entity_code'))

df_bci2$difference <-  df_bci2$b_capacity.y - df_bci2$b_capacity.x 

# Plot differences in BCI from 2016 to 2018
df_bci2 %>%
  ggplot(aes(x = reorder(entity_code, difference), y = difference)) +
  geom_bar(stat="identity", alpha=.6, width=.4) +
  coord_flip() +
  xlab("Municipality") +
  ylab("Difference in BCI between 2018 and 2016") +
  theme_bw()


# Figure B2: IGM Correlations

## New dataframe: Municipality-period level; Components of the Index of Municipal Management
df_muni <- df %>%
  group_by(entity_code,mayor_period) %>%
  summarise_at(vars(igm,iga,ige,igf,iic,ipc,isp), 
               list(muni = max)) 

df_muni <- df_muni%>% 
  rename(IGM = igm_muni, 
         IGA = iga_muni, 
         IGE = ige_muni, 
         IGF = igf_muni,
         IIC = iic_muni,
         IPC = ipc_muni,
         ISP = isp_muni)

## Plot of correlations between the components of the Index of Municipal Management
library(ggcorrplot)

corr_matrix <- cor(df_muni[, c("IGA","IGE","IGF","IGM","IIC","IPC","ISP")])
ggcorrplot(corr_matrix) + guides(fill=guide_legend(title="Correlation"))


# Figure B3: Principal Component Analysis: Visualizations

library(factoextra)

## Subset containing just the indices of the IGM
dfsubpca <- subset(df_muni, select = c("IGA","IGE","IGF","IIC","IPC","ISP"))


## PCA based on singular value decomposition (SVD)
pca_model <- prcomp(dfsubpca, scale. = TRUE, center = TRUE)
summary(pca_model)

## Importance of components:
##                         PC1    PC2    PC3     PC4     PC5     PC6
##Standard deviation     1.7130 1.0188 0.8367 0.71379 0.66308 0.61517
##Proportion of Variance 0.4891 0.1730 0.1167 0.08492 0.07328 0.06307
##Cumulative Proportion  0.4891 0.6621 0.7787 0.86365 0.93693 1.00000

## (a) Proportion of variance explained by each principal component.
fviz_eig(pca_model) + labs(title="Proportion of Variance Explained by each Component")

## (b) Visualize the contribution of each variable to Component 1
fviz_cos2(pca_model, choice = "var", axes = 1:1) + # iga & ige > 0.6
  labs(title="Contribution of each Variable to Component 1")

## (c) Visualize the contribution of each variable to Component 2
fviz_cos2(pca_model, choice = "var", axes = 2:2) + # igf > 06
  labs(title="Contribution of each Variable to Component 2")

## (d) Visualize the contribution of each variable to Component 3
fviz_cos2(pca_model, choice = "var", axes = 3:3) + # ipc > 0.4
  labs(title="Contribution of each Variable to Component 3")

## (e) Visualize the contribution of each variable to Component 4
fviz_cos2(pca_model, choice = "var", axes = 4:4) + # isp > 0.3
  labs(title="Contribution of each Variable to Component 4")

## (f) Visualize the contribution of each variable to Component 5
fviz_cos2(pca_model, choice = "var", axes = 5:5) + # iga & ige > 0.1 
  labs(title="Contribution of each Variable to Component 5")


# Table B4: Correlates of the Municipal Management Index’s Components

## Importing municipal-level data (only available for 2018)
df_igm_icl <- read.csv("IGM_ICL_2018.csv", stringsAsFactors = FALSE)
df_igm_icl <- na.omit(df_igm_icl)
ls(df_igm_icl)

## Replicating the Bureaucratic Capabilities Index
df_igm_icl$bci <- (df_igm_icl$iga + df_igm_icl$ige)/2

m_bci <- lm(data = df_igm_icl, bci ~ log(gdp_pc_2017) + log(population) + log(edu_publicexp_pc) + log(hel_publicexp_pc) + urbanization_rate + as.factor(cod_dep))
m_bci$vcov <- vcovCL(m_bci, cluster = ~ cod_dep)
m_bci_coef <- coeftest(m_bci, m_bci$vcov)
m_bci_cse <- sqrt(diag(m_bci$vcov))     

m_igm <- lm(data = df_igm_icl, gm_index ~ log(gdp_pc_2017) + log(population) + log(edu_publicexp_pc) + log(hel_publicexp_pc) + urbanization_rate + as.factor(cod_dep))
m_igm$vcov <- vcovCL(m_igm, cluster = ~ cod_dep)
m_igm_coef <- coeftest(m_igm, m_igm$vcov)
m_igm_cse <- sqrt(diag(m_igm$vcov))  

m_iga <- lm(data = df_igm_icl, iga ~ log(gdp_pc_2017) + log(population) + log(edu_publicexp_pc) + log(hel_publicexp_pc) + urbanization_rate + as.factor(cod_dep))
m_iga$vcov <- vcovCL(m_iga, cluster = ~ cod_dep)
m_iga_coef <- coeftest(m_iga, m_iga$vcov)
m_iga_cse <- sqrt(diag(m_iga$vcov))   

m_ige <- lm(data = df_igm_icl, ige ~ log(gdp_pc_2017) + log(population) + log(edu_publicexp_pc) + log(hel_publicexp_pc) + urbanization_rate + as.factor(cod_dep))
m_ige$vcov <- vcovCL(m_ige, cluster = ~ cod_dep)
m_ige_coef <- coeftest(m_ige, m_ige$vcov)
m_ige_cse <- sqrt(diag(m_ige$vcov))  

m_igf <- lm(data = df_igm_icl, igf ~ log(gdp_pc_2017) + log(population) + log(edu_publicexp_pc) + log(hel_publicexp_pc) + urbanization_rate + as.factor(cod_dep))
m_igf$vcov <- vcovCL(m_igf, cluster = ~ cod_dep)
m_igf_coef <- coeftest(m_igf, m_igf$vcov)
m_igf_cse <- sqrt(diag(m_igf$vcov))     

m_iic <- lm(data = df_igm_icl, iic ~ log(gdp_pc_2017) + log(population) + log(edu_publicexp_pc) + log(hel_publicexp_pc) + urbanization_rate + as.factor(cod_dep))
m_iic$vcov <- vcovCL(m_iic, cluster = ~ cod_dep)
m_iic_coef <- coeftest(m_iic, m_iic$vcov)
m_iic_cse <- sqrt(diag(m_iic$vcov))  

m_isp <- lm(data = df_igm_icl, isp ~ log(gdp_pc_2017) + log(population) + log(edu_publicexp_pc) + log(hel_publicexp_pc) + urbanization_rate + as.factor(cod_dep))
m_isp$vcov <- vcovCL(m_isp, cluster = ~ cod_dep)
m_isp_coef <- coeftest(m_isp, m_isp$vcov)
m_isp_cse <- sqrt(diag(m_isp$vcov))    

m_ipc <- lm(data = df_igm_icl, ipc ~ log(gdp_pc_2017) + log(population) + log(edu_publicexp_pc) + log(hel_publicexp_pc) + urbanization_rate + as.factor(cod_dep))
m_ipc$vcov <- vcovCL(m_ipc, cluster = ~ cod_dep)
m_ipc_coef <- coeftest(m_ipc, m_ipc$vcov)
m_ipc_cse <- sqrt(diag(m_ipc$vcov))      


# TABLE with clustered standard errors:
stargazer(m_bci, m_igm, m_iga, m_ige, m_igf, m_iic, m_isp, m_ipc,
          se = list(m_bci_cse, m_igm_cse, m_iga_cse, m_ige_cse, m_igf_cse, m_iic_cse, m_isp_cse, m_ipc_cse), 
          title="Correlates of the Municipal Management Index's Components",
          align=TRUE, 
          dep.var.labels=c("index")
          , star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"),
          no.space=TRUE, add.lines = list(c("Department FE", "Y","Y","Y","Y","Y","Y","Y","Y")),  omit = c("cod_dep"))



# Table B5: Political Connections, Tests of the Measure of Bureaucrats’ Capabilities and Procurement Awards

## Loadings:
pca_model$rotation

## Predicting scores
df <- df%>% 
  rename(IGM = igm, 
         IGA = iga, 
         IGE = ige, 
         IGF = igf,
         IIC = iic,
         IPC = ipc,
         ISP = isp)
df_igm <- subset(df, select = c("IGA","IGE","IGF","IIC","IPC","ISP"))
pred_scores <- predict(pca_model, newdata = df_igm)

## Creating placebo BCI:
df$placebobci <- pred_scores[,3]*(0.1167/(0.1167+0.08492)) + pred_scores[,4]*(0.08492/(0.1167+0.08492))
summary(df$placebobci)

## Creating BCI based on first component 
df$bci_pca1 <- -1*pred_scores[,1]
summary(df$bci_pca1)

## Linear models, placebo BCI
## Sample of common support between connected and not.
dft_p1 <- df[ which(df$placebobci<=0.6), ]
dft_p1 <- dft_p1[ which(dft_p1$placebobci>=-0.6), ]

## First: Placebo test, Model without controls and without FEs
placebomodel1 <- lm(award ~ placebobci + connectedbin + placebobci:connectedbin, data = dft_p1)
placebomodel1$vcov <- vcovCL(placebomodel1, cluster = ~ entity_code + year + nit_bidder)
placebomodel1_coef <- coeftest(placebomodel1, placebomodel1$vcov)
placebomodel1_cse <- sqrt(diag(placebomodel1$vcov))

## Second: Placebo test, Model with controls but no FEs
placebomodel2 <- lm(award ~ placebobci + connectedbin + placebobci:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president, data = dft_p1)
placebomodel2$vcov <- vcovCL(placebomodel2, cluster = ~ entity_code + year + nit_bidder)
placebomodel2_coef <- coeftest(placebomodel2, placebomodel2$vcov)
placebomodel2_cse <- sqrt(diag(placebomodel2$vcov))

## Third: Placebo test, Model with controls and FEs for Year and Municipality
placebomodel3 <- lm(award ~ placebobci + connectedbin + placebobci:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_p1)
placebomodel3$vcov <- vcovCL(placebomodel3, cluster = ~ entity_code + year + nit_bidder)
placebomodel3_coef <- coeftest(placebomodel3, placebomodel3$vcov)
placebomodel3_cse <- sqrt(diag(placebomodel3$vcov))

## Linear models, alternative measure of BCI: scores from first component of the PCA
## Sample of common support between connected and not.
dft_p2 <- df[ which(df$bci_pca1<=3), ]
dft_p2 <- dft_p2[ which(dft_p2$bci_pca1>=-2), ]

## First: Alternative measure of BCI, Model without controls and without FEs
model1 <- lm(award ~ bci_pca1 + connectedbin + bci_pca1:connectedbin, data = dft_p2)
model1$vcov <- vcovCL(model1, cluster = ~ entity_code + year + nit_bidder)
model1_coef <- coeftest(model1, model1$vcov)
model1_cse <- sqrt(diag(model1$vcov))

## Second: Alternative measure of BCI, Model with controls but no FEs
model2 <- lm(award ~ bci_pca1 + connectedbin + bci_pca1:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president, data = dft_p2)
model2$vcov <- vcovCL(model2, cluster = ~ entity_code + year + nit_bidder)
model2_coef <- coeftest(model2, model2$vcov)
model2_cse <- sqrt(diag(model2$vcov))

## Third: Alternative measure of BCI, Model with controls and FEs for Year and Municipality
model3 <- lm(award ~ bci_pca1 + connectedbin + bci_pca1:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_p2)
model3$vcov <- vcovCL(model3, cluster = ~ entity_code + year + nit_bidder)
model3_coef <- coeftest(model3, model3$vcov)
model3_cse <- sqrt(diag(model3$vcov))

## TABLE with clustered standard errors:
stargazer(placebomodel1, placebomodel2, placebomodel3, model1, model2, model3, 
          se = list(placebomodel1_cse, placebomodel2_cse, placebomodel3_cse, model1_cse, model2_cse, model3_cse) , title="Political Connections, Tests of the Measure of Bureaucrats' Capabilities and Procurement Awards",
          align=TRUE, dep.var.labels=c("Award"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"),
          no.space=TRUE, add.lines = list(c("Controls", " ","Y","Y"," ","Y","Y"),
                                          c("Municipality FE"," ","","Y"," ","","Y"),
                                          c("Year FE"," ","","Y"," ","","Y")),  keep = c("Constant",  "placebobci", "bci_pca1", "connectedbin"))


# Table B6: Political Connections, Tests of the Measure of Bureaucrats’ Capabilities and Procurement Awards (Logistic Regressions)

## Logit models, placebo BCI
## Sample of common support between connected and not.
dft_p1 <- df[ which(df$placebobci<=0.6), ]
dft_p1 <- dft_p1[ which(dft_p1$placebobci>=-0.6), ]

## First: Placebo test, Model without controls and without FEs
l_placebomodel1 <- glm(award ~ placebobci + connectedbin + placebobci:connectedbin, data = dft_p1, family = binomial)
l_placebomodel1$vcov <- vcovCL(l_placebomodel1, cluster = ~ entity_code + year + nit_bidder)
l_placebomodel1_coef <- coeftest(l_placebomodel1, l_placebomodel1$vcov)
l_placebomodel1_cse <- sqrt(diag(l_placebomodel1$vcov))

## Second: Placebo test, Model with controls but no FEs
l_placebomodel2 <- glm(award ~ placebobci + connectedbin + placebobci:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president, data = dft_p1, family = binomial)
l_placebomodel2$vcov <- vcovCL(l_placebomodel2, cluster = ~ entity_code + year + nit_bidder)
l_placebomodel2_coef <- coeftest(l_placebomodel2, l_placebomodel2$vcov)
l_placebomodel2_cse <- sqrt(diag(l_placebomodel2$vcov))

## Third: Placebo test, Model with controls and FEs for Year and Municipality
l_placebomodel3 <- glm(award ~ placebobci + connectedbin + placebobci:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_p1, family = binomial)
l_placebomodel3$vcov <- vcovCL(l_placebomodel3, cluster = ~ entity_code + year + nit_bidder)
l_placebomodel3_coef <- coeftest(l_placebomodel3, l_placebomodel3$vcov)
l_placebomodel3_cse <- sqrt(diag(l_placebomodel3$vcov))


## Logit models, alternative measure of BCI: scores from first component of the PCA
## Sample of common support between connected and not.
dft_p2 <- df[ which(df$bci_pca1<=3), ]
dft_p2 <- dft_p2[ which(dft_p2$bci_pca1>=-2), ]

## First: Alternative measure of BCI, Model without controls and without FEs
l_model1 <- glm(award ~ bci_pca1 + connectedbin + bci_pca1:connectedbin, data = dft_p2, family = binomial)
l_model1$vcov <- vcovCL(l_model1, cluster = ~ entity_code + year + nit_bidder)
l_model1_coef <- coeftest(l_model1, l_model1$vcov)
l_model1_cse <- sqrt(diag(l_model1$vcov))

## Second: Alternative measure of BCI, Model with controls but no FEs
l_model2 <- glm(award ~ bci_pca1 + connectedbin + bci_pca1:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president, data = dft_p2, family = binomial)
l_model2$vcov <- vcovCL(l_model2, cluster = ~ entity_code + year + nit_bidder)
l_model2_coef <- coeftest(l_model2, l_model2$vcov)
l_model2_cse <- sqrt(diag(l_model2$vcov))

## Third: Alternative measure of BCI, Model with controls and FEs for Year and Municipality
l_model3 <- glm(award ~ bci_pca1 + connectedbin + bci_pca1:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_p2, family = binomial)
l_model3$vcov <- vcovCL(l_model3, cluster = ~ entity_code + year + nit_bidder)
l_model3_coef <- coeftest(l_model3, l_model3$vcov)
l_model3_cse <- sqrt(diag(l_model3$vcov))


# TABLE with clustered standard errors:
stargazer(l_placebomodel1, l_placebomodel2, l_placebomodel3, l_model1, l_model2, l_model3, 
          se = list(l_placebomodel1_cse, l_placebomodel2_cse, l_placebomodel3_cse, l_model1_cse, l_model2_cse, l_model3_cse) , title="PPolitical Connections, Tests of the Measure of Bureaucrats' Capabilities and Procurement Awards (Logit)",
          align=TRUE, dep.var.labels=c("Award"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"),
          no.space=TRUE, add.lines = list(c("Controls", " ","Y","Y"," ","Y","Y"),
                                          c("Municipality FE", " ","","Y"," ","","Y"),
                                          c("Year FE"," ","","Y"," ","","Y")),  keep = c("Constant",  "placebobci", "bci_pca1", "connectedbin"))



# Table B7: Political Connections, Tests of the Measure of Bureaucrats’ Capabilities and Awards to Single Bidders

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Linear models, placebo BCI
## Sample of common support between connected and not.
dft_nog_p1 <- df_nog[ which(df_nog$placebobci<=0.9), ]
dft_nog_p1 <- dft_nog_p1[ which(dft_nog_p1$placebobci>=-1.1), ]

## First: Placebo test, Model without controls and without FEs
pm_sb_1 <- lm(risky_award ~ placebobci + connectedbin + placebobci:connectedbin, data = dft_nog_p1)
pm_sb_1$vcov <- vcovCL(pm_sb_1, cluster = ~ entity_code + year + nit_bidder)
pm_sb_1_coef <- coeftest(pm_sb_1, pm_sb_1$vcov)
pm_sb_1_cse <- sqrt(diag(pm_sb_1$vcov))

## Second: Placebo test, Model with controls but no FEs
pm_sb_2 <- lm(risky_award ~ placebobci + connectedbin + placebobci:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president, data = dft_nog_p1)
pm_sb_2$vcov <- vcovCL(pm_sb_2, cluster = ~ entity_code + year + nit_bidder)
pm_sb_2_coef <- coeftest(pm_sb_2, pm_sb_2$vcov)
pm_sb_2_cse <- sqrt(diag(pm_sb_2$vcov))

## Third: Placebo test, Model with controls and FEs for Year and Municipality
pm_sb_3 <- lm(risky_award ~ placebobci + connectedbin + placebobci:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_nog_p1)
pm_sb_3$vcov <- vcovCL(pm_sb_3, cluster = ~ entity_code + year + nit_bidder)
pm_sb_3_coef <- coeftest(pm_sb_3, pm_sb_3$vcov)
pm_sb_3_cse <- sqrt(diag(pm_sb_3$vcov))

## Linear models, alternative measure of BCI: scores from first component of the PCA
## Sample of common support between connected and not.
dft_nog_p2 <- df_nog[ which(df_nog$bci_pca1<=3), ]
dft_nog_p2 <- dft_nog_p2[ which(dft_nog_p2$bci_pca1>=-2.4), ]

## First: Alternative measure of BCI, Model without controls and without FEs
altm_sb_1 <- lm(risky_award ~ bci_pca1 + connectedbin + bci_pca1:connectedbin, data = dft_nog_p2)
altm_sb_1$vcov <- vcovCL(altm_sb_1, cluster = ~ entity_code + year + nit_bidder)
altm_sb_1_coef <- coeftest(altm_sb_1, altm_sb_1$vcov)
altm_sb_1_cse <- sqrt(diag(altm_sb_1$vcov))

## Second: Alternative measure of BCI, Model with controls but no FEs
altm_sb_2 <- lm(risky_award ~ bci_pca1 + connectedbin + bci_pca1:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president, data = dft_nog_p2)
altm_sb_2$vcov <- vcovCL(altm_sb_2, cluster = ~ entity_code + year + nit_bidder)
altm_sb_2_coef <- coeftest(altm_sb_2, altm_sb_2$vcov)
altm_sb_2_cse <- sqrt(diag(altm_sb_2$vcov))

## Third: Alternative measure of BCI, Model with controls and FEs for Year and Municipality
altm_sb_3 <- lm(risky_award ~ bci_pca1 + connectedbin + bci_pca1:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_nog_p2)
altm_sb_3$vcov <- vcovCL(altm_sb_3, cluster = ~ entity_code + year + nit_bidder)
altm_sb_3_coef <- coeftest(altm_sb_3, altm_sb_3$vcov)
altm_sb_3_cse <- sqrt(diag(altm_sb_3$vcov))


# TABLE with clustered standard errors:
stargazer(pm_sb_1, pm_sb_2, pm_sb_3, altm_sb_1, altm_sb_2, altm_sb_3, 
          se = list(pm_sb_1_cse, pm_sb_2_cse, pm_sb_3_cse, altm_sb_1_cse, altm_sb_2_cse, altm_sb_3_cse) , title="Political Connections, Tests of the Measure of Bureaucrats' Capabilities and Awards to Single Bidders",
          align=TRUE, dep.var.labels=c("Award to Single Bidder"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"),
          no.space=TRUE, add.lines = list(c("Controls", " ","Y","Y"," ","Y","Y"),
                                          c("Municipality FE"," ","","Y"," ","","Y"),
                                          c("Year FE"," ","","Y"," ","","Y")),  keep = c("Constant",  "placebobci", "bci_pca1", "connectedbin"))


# Table B8: Political Connections, Tests of the Measure of Bureaucrats’ Capabilities and Awards to Single Bidders (Logistic Regression)

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Logit models, placebo BCI
## Sample of common support between connected and not.
dft_nog_p1 <- df_nog[ which(df_nog$placebobci<=0.9), ]
dft_nog_p1 <- dft_nog_p1[ which(dft_nog_p1$placebobci>=-1.1), ]

## First: Placebo test, Model without controls and without FEs
l_pm_sb_1 <- glm(risky_award ~ placebobci + connectedbin + placebobci:connectedbin, data = dft_nog_p1, family = binomial)
l_pm_sb_1$vcov <- vcovCL(l_pm_sb_1, cluster = ~ entity_code + year + nit_bidder)
l_pm_sb_1_coef <- coeftest(l_pm_sb_1, l_pm_sb_1$vcov)
l_pm_sb_1_cse <- sqrt(diag(l_pm_sb_1$vcov))

## Second: Placebo test, Model with controls but no FEs
l_pm_sb_2 <- glm(risky_award ~ placebobci + connectedbin + placebobci:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president, data = dft_nog_p1, family = binomial)
l_pm_sb_2$vcov <- vcovCL(l_pm_sb_2, cluster = ~ entity_code + year + nit_bidder)
l_pm_sb_2_coef <- coeftest(l_pm_sb_2, l_pm_sb_2$vcov)
l_pm_sb_2_cse <- sqrt(diag(l_pm_sb_2$vcov))

## Third: Placebo test, Model with controls and FEs for Year and Municipality
l_pm_sb_3 <- glm(risky_award ~ placebobci + connectedbin + placebobci:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_nog_p1, family = binomial)
l_pm_sb_3$vcov <- vcovCL(l_pm_sb_3, cluster = ~ entity_code + year + nit_bidder)
l_pm_sb_3_coef <- coeftest(l_pm_sb_3, l_pm_sb_3$vcov)
l_pm_sb_3_cse <- sqrt(diag(l_pm_sb_3$vcov))


## Logit models, alternative measure of BCI: scores from first component of the PCA
## Sample of common support between connected and not.
dft_nog_p2 <- df_nog[ which(df_nog$bci_pca1<=3), ]
dft_nog_p2 <- dft_nog_p2[ which(dft_nog_p2$bci_pca1>=-2.4), ]

## First: Alternative measure of BCI, Model without controls and without FEs
l_altm_sb_1 <- glm(risky_award ~ bci_pca1 + connectedbin + bci_pca1:connectedbin, data = dft_nog_p2, family = binomial)
l_altm_sb_1$vcov <- vcovCL(l_altm_sb_1, cluster = ~ entity_code + year + nit_bidder)
l_altm_sb_1_coef <- coeftest(l_altm_sb_1, l_altm_sb_1$vcov)
l_altm_sb_1_cse <- sqrt(diag(l_altm_sb_1$vcov))

## Second: Alternative measure of BCI, Model with controls but no FEs
l_altm_sb_2 <- glm(risky_award ~ bci_pca1 + connectedbin + bci_pca1:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president, data = dft_nog_p2, family = binomial)
l_altm_sb_2$vcov <- vcovCL(l_altm_sb_2, cluster = ~ entity_code + year + nit_bidder)
l_altm_sb_2_coef <- coeftest(l_altm_sb_2, l_altm_sb_2$vcov)
l_altm_sb_2_cse <- sqrt(diag(l_altm_sb_2$vcov))

## Third: Alternative measure of BCI, Model with controls and FEs for Year and Municipality
l_altm_sb_3 <- glm(risky_award ~ bci_pca1 + connectedbin + bci_pca1:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_nog_p2, family = binomial)
l_altm_sb_3$vcov <- vcovCL(l_altm_sb_3, cluster = ~ entity_code + year + nit_bidder)
l_altm_sb_3_coef <- coeftest(l_altm_sb_3, l_altm_sb_3$vcov)
l_altm_sb_3_cse <- sqrt(diag(l_altm_sb_3$vcov))


# TABLE with clustered standard errors:
stargazer(l_pm_sb_1, l_pm_sb_2, l_pm_sb_3, l_altm_sb_1, l_altm_sb_2, l_altm_sb_3, 
          se = list(l_pm_sb_1_cse, l_pm_sb_2_cse, l_pm_sb_3_cse, l_altm_sb_1_cse, l_altm_sb_2_cse, l_altm_sb_3_cse) , title="Political Connections, Tests of the Measure of Bureaucrats' Capabilities and Awards to Single Bidders (Logit)",
          align=TRUE, dep.var.labels=c("Award to Single Bidder"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"),
          no.space=TRUE, add.lines = list(c("Controls", " ","Y","Y"," ","Y","Y"),
                                          c("Municipality FE", " ","","Y"," ","","Y"),
                                          c("Year FE"," ","","Y"," ","","Y")),  keep = c("Constant",  "placebobci", "bci_pca1", "connectedbin"))



# Table B9: Political Connections, Tests of the Measure of Bureaucrats’ Capabilities and Number of Bids

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Linear models, placebo BCI
## Sample of common support between connected and not.
dft_nog_p1 <- df_nog[ which(df_nog$placebobci<=0.9), ]
dft_nog_p1 <- dft_nog_p1[ which(dft_nog_p1$placebobci>=-1.1), ]

## First: Placebo test, Model without controls and without FEs
pm_nb_1 <- lm(num_bids ~ placebobci + connectedbin + placebobci:connectedbin, data = dft_nog_p1)
pm_nb_1$vcov <- vcovCL(pm_nb_1, cluster = ~ entity_code + year + nit_bidder)
pm_nb_1_coef <- coeftest(pm_nb_1, pm_nb_1$vcov)
pm_nb_1_cse <- sqrt(diag(pm_nb_1$vcov))

## Second: Placebo test, Model with controls but no FEs
pm_nb_2 <- lm(num_bids ~ placebobci + connectedbin + placebobci:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president, data = dft_nog_p1)
pm_nb_2$vcov <- vcovCL(pm_nb_2, cluster = ~ entity_code + year + nit_bidder)
pm_nb_2_coef <- coeftest(pm_nb_2, pm_nb_2$vcov)
pm_nb_2_cse <- sqrt(diag(pm_nb_2$vcov))

## Third: Placebo test, Model with controls and FEs for Year and Municipality
pm_nb_3 <- lm(num_bids ~ placebobci + connectedbin + placebobci:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_nog_p1)
pm_nb_3$vcov <- vcovCL(pm_nb_3, cluster = ~ entity_code + year + nit_bidder)
pm_nb_3_coef <- coeftest(pm_nb_3, pm_nb_3$vcov)
pm_nb_3_cse <- sqrt(diag(pm_nb_3$vcov))


## Linear models, alternative measure of BCI: scores from first component of the PCA
## Sample of common support between connected and not.
dft_nog_p2 <- df_nog[ which(df_nog$bci_pca1<=3), ]
dft_nog_p2 <- dft_nog_p2[ which(dft_nog_p2$bci_pca1>=-2.4), ]

## First: Alternative measure of BCI, Model without controls and without FEs
altm_nb_1 <- lm(num_bids ~ bci_pca1 + connectedbin + bci_pca1:connectedbin, data = dft_nog_p2)
altm_nb_1$vcov <- vcovCL(altm_nb_1, cluster = ~ entity_code + year + nit_bidder)
altm_nb_1_coef <- coeftest(altm_nb_1, altm_nb_1$vcov)
altm_nb_1_cse <- sqrt(diag(altm_nb_1$vcov))

## Second: Alternative measure of BCI, Model with controls but no FEs
altm_nb_2 <- lm(num_bids ~ bci_pca1 + connectedbin + bci_pca1:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president, data = dft_nog_p2)
altm_nb_2$vcov <- vcovCL(altm_nb_2, cluster = ~ entity_code + year + nit_bidder)
altm_nb_2_coef <- coeftest(altm_nb_2, altm_nb_2$vcov)
altm_nb_2_cse <- sqrt(diag(altm_nb_2$vcov))

## Third: Alternative measure of BCI, Model with controls and FEs for Year and Municipality
altm_nb_3 <- lm(num_bids ~ bci_pca1 + connectedbin + bci_pca1:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + logpopulation + party_president + factor(entity_code) + factor(year), data = dft_nog_p2)
altm_nb_3$vcov <- vcovCL(altm_nb_3, cluster = ~ entity_code + year + nit_bidder)
altm_nb_3_coef <- coeftest(altm_nb_3, altm_nb_3$vcov)
altm_nb_3_cse <- sqrt(diag(altm_nb_3$vcov))


# TABLE with clustered standard errors:
stargazer(pm_nb_1, pm_nb_2, pm_nb_3, altm_nb_1, altm_nb_2, altm_nb_3, 
          se = list(pm_nb_1_cse, pm_nb_2_cse, pm_nb_3_cse, altm_nb_1_cse, altm_nb_2_cse, altm_nb_3_cse) , title="Political Connections, Tests of the Measure of Bureaucrats' Capabilities and Number of Bids",
          align=TRUE, dep.var.labels=c("Number of Bids"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"),
          no.space=TRUE, add.lines = list(c("Controls", " ","Y","Y"," ","Y","Y"),
                                          c("Municipality FE"," ","","Y"," ","","Y"),
                                          c("Year FE"," ","","Y"," ","","Y")),  keep = c("Constant",  "placebobci", "bci_pca1", "connectedbin"))




# Figure B4: Political Connections, Alternative Bureaucrats’ Capabilities, and Procurement Awards (Binning Estimator)

## Panel (A) Alt. Capabilities Index (BCI using IGA + IGE + IGF)
comb02_award_plot_binning <- interflex(Y = "award", D = "connectedbin", X = "b_capacity_alt", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df[df$b_capacity_alt<=0.52,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 1)", theme.bw = TRUE, na.rm = TRUE)
plot(comb02_award_plot_binning,theme.bw = TRUE)

## Panel (B) IGA
comb12_award_plot_binning <- interflex(Y = "award", D = "connectedbin", X = "iga", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df[df$iga<=.8,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 2)", theme.bw = TRUE, na.rm = TRUE)
plot(comb12_award_plot_binning,theme.bw = TRUE)

## Panel (C) IGE
comb22_award_plot_binning <- interflex(Y = "award", D = "connectedbin", X = "ige", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df[df$ige<=.50,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 3)", theme.bw = TRUE, na.rm = TRUE)
plot(comb22_award_plot_binning,theme.bw = TRUE)

## Panel (D) IGF
comb32_award_plot_binning <- interflex(Y = "award", D = "connectedbin", X = "igf", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df[df$igf>=.22 & df$igf<=.4,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 4)", theme.bw = TRUE, na.rm = TRUE)
plot(comb32_award_plot_binning,theme.bw = TRUE)


# Figure B5: Linear Interaction Diagnostics Plots - Procurement Contracts and Alternative Bureaucrats’ Capabilities Measures

## Panel (A) Alt. Capabilities Index (BCI using IGA + IGE + IGF)
comb02 <- interflex(estimator = "raw", Y = "award", D = "connectedbin", X = "b_capacity_alt", ncols=2, data = df, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 1)", theme.bw = TRUE)
plot(comb02,theme.bw = TRUE)

## Panel (B) IGA
comb12 <- interflex(estimator = "raw", Y = "award", D = "connectedbin", X = "iga", ncols=2, data = df, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 2)", theme.bw = TRUE)
plot(comb12,theme.bw = TRUE)

## Panel (C) IGE
comb22 <- interflex(estimator = "raw", Y = "award", D = "connectedbin", X = "ige", ncols=2, data = df, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 3)", theme.bw = TRUE)
plot(comb22,theme.bw = TRUE)

## Panel (D) IGF
comb32 <- interflex(estimator = "raw", Y = "award", D = "connectedbin", X = "igf", ncols=2, data = df, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt.4)", theme.bw = TRUE)
plot(comb32,theme.bw = TRUE)



# Table B10: Political Connections, Bureaucrats’ Capabilities and Awards (Alternative Measures of Bureaucrats’ Capabilities)

## Linear Probability Models
## Model_0: Model with controls and FEs; Main connections (binary) + BCI using IGA + IGE + IGF
acon2_lm0 <- lm(award ~ b_capacity_alt + connectedbin + b_capacity_alt:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[df$b_capacity_alt<=0.52,])
acon2_lm0$vcov <- vcovCL(acon2_lm0, cluster = ~ entity_code + year + nit_bidder)
acon2_lm0_coef = coeftest(acon2_lm0, acon2_lm0$vcov)
acon2_lm0_cse <- sqrt(diag(acon2_lm0$vcov))

## Model 1: Model with controls and FEs; Main connections (binary) + BCI using IGA 
acon2_lm1 <- lm(award ~ iga + connectedbin + iga:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[df$iga<=.8,])
acon2_lm1$vcov <- vcovCL(acon2_lm1, cluster = ~ entity_code + year + nit_bidder)
acon2_lm1_coef = coeftest(acon2_lm1, acon2_lm1$vcov)
acon2_lm1_cse <- sqrt(diag(acon2_lm1$vcov))

## Model 2: Model with controls and FEs; Main connections (binary) + BCI using IGE 
acon2_lm2 <- lm(award ~ ige + connectedbin + ige:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[df$ige<=.50,])
acon2_lm2$vcov <- vcovCL(acon2_lm2, cluster = ~ entity_code + year + nit_bidder)
acon2_lm2_coef = coeftest(acon2_lm2, acon2_lm2$vcov)
acon2_lm2_cse <- sqrt(diag(acon2_lm2$vcov))

## Model 3: Model with controls and FEs; Main connections (binary) + BCI using IGF
acon2_lm3 <- lm(award ~ igf + connectedbin + igf:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[df$igf>=.22 & df$igf<=.4,])
acon2_lm3$vcov <- vcovCL(acon2_lm3, cluster = ~ entity_code + year + nit_bidder)
acon2_lm3_coef = coeftest(acon2_lm3, acon2_lm3$vcov)
acon2_lm3_cse <- sqrt(diag(acon2_lm3$vcov))


## Logistics Regressions
## Model_0: Model with controls and FEs; Main connections (binary) + BCI using IGA + IGE + IGF
acon2_0 <- glm(award ~ b_capacity_alt + connectedbin + b_capacity_alt:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[df$b_capacity_alt<=0.52,], family = binomial)
acon2_0$vcov <- vcovCL(acon2_0, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
acon2_0_coef = coeftest(acon2_0, acon2_0$vcov)
acon2_0_cse <- sqrt(diag(acon2_0$vcov))

## Model 1: Model with controls and FEs; Main connections (binary) + BCI using IGA 
acon2_1 <- glm(award ~ iga + connectedbin + iga:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[df$iga<=.8,], family = binomial)
acon2_1$vcov <- vcovCL(acon2_1, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
acon2_1_coef = coeftest(acon2_1, acon2_1$vcov)
acon2_1_cse <- sqrt(diag(acon2_1$vcov))

## Model 2: Model with controls and FEs; Main connections (binary) + BCI using IGE 
acon2_2 <- glm(award ~ ige + connectedbin + ige:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[df$ige<=.50,], family = binomial)
acon2_2$vcov <- vcovCL(acon2_2, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
acon2_2_coef = coeftest(acon2_2, acon2_2$vcov)
acon2_2_cse <- sqrt(diag(acon2_2$vcov))

## Model 3: Model with controls and FEs; Main connections (binary) + BCI using IGF
acon2_3 <- glm(award ~ igf + connectedbin + igf:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[df$igf>=.22 & df$igf<=.4,], family = binomial)
acon2_3$vcov <- vcovCL(acon2_3, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
acon2_3_coef = coeftest(acon2_3, acon2_3$vcov)
acon2_3_cse <- sqrt(diag(acon2_3$vcov))


# TABLE
stargazer(acon2_lm0, acon2_0, acon2_lm1, acon2_1, acon2_lm2, acon2_2, acon2_lm3, acon2_3, se = list(acon2_lm0_cse, acon2_0_cse, acon2_lm1_cse, acon2_1_cse, acon2_lm2_cse, acon2_2_cse, acon2_lm3_cse, acon2_3_cse), 
          title="Connections, Bureaucrats' Capabilities and Awards (Alternatie Measures of Bureaucrats' Capabilities)",
          align=TRUE, dep.var.labels=c("award"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"), 
          no.space=TRUE, add.lines = list(c("Controls", "Y","Y","Y","Y","Y","Y","Y","Y"),
                                          c("Municipality FE", "Y","Y","Y","Y","Y","Y","Y","Y"),
                                          c("Year FE","Y","Y","Y","Y","Y","Y","Y","Y")),  keep = c("Constant","connectedbin","b_capacity_alt","iga","ige","igf"))



# Figure B6: Political Connections, Alternative Bureaucrats’ Capabilities, and Awards to Single Bidders (Binning Estimator)

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Panel (A) Alt. Capabilities Index
rcomb02_award_plot_binning <- interflex(Y = "risky_award", D = "connectedbin", X = "b_capacity_alt", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[df_nog$b_capacity_alt<=0.52,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 1)", theme.bw = TRUE, na.rm = TRUE)
plot(rcomb02_award_plot_binning,theme.bw = TRUE)

## Panel (B) IGA
rcomb12_award_plot_binning <- interflex(Y = "risky_award", D = "connectedbin", X = "iga", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[df_nog$iga<=.8,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 2)", theme.bw = TRUE, na.rm = TRUE)
plot(rcomb12_award_plot_binning,theme.bw = TRUE)

## Panel (C) IGE
rcomb22_award_plot_binning <- interflex(Y = "risky_award", D = "connectedbin", X = "ige", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[df_nog$ige<=.50,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 3)", theme.bw = TRUE, na.rm = TRUE)
plot(rcomb22_award_plot_binning,theme.bw = TRUE)

## Panel (D) IGF
rcomb32_award_plot_binning <- interflex(Y = "risky_award", D = "connectedbin", X = "igf", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[df_nog$igf>=.22 & df_nog$igf<=.4,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 4)", theme.bw = TRUE, na.rm = TRUE)
plot(rcomb32_award_plot_binning,theme.bw = TRUE)



# Figure B7: Political Connections, Alternative Bureaucrats’ Capabilities, and the Number of Bids (Binning Estimator)

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Panel (A) Alt. Capabilities Index
nbcomb02_award_plot_binning <- interflex(Y = "num_bids", D = "connectedbin", X = "b_capacity_alt", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[df_nog$b_capacity_alt<=0.52,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 1)", theme.bw = TRUE, na.rm = TRUE)
plot(nbcomb02_award_plot_binning,theme.bw = TRUE)

## Panel (B) IGA
nbcomb12_award_plot_binning <- interflex(Y = "num_bids", D = "connectedbin", X = "iga", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[df_nog$iga<=.8,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 2)", theme.bw = TRUE, na.rm = TRUE)
plot(nbcomb12_award_plot_binning,theme.bw = TRUE)

## Panel (C) IGE
nbcomb22_award_plot_binning <- interflex(Y = "num_bids", D = "connectedbin", X = "ige", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[df_nog$ige<=.50,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 3)", theme.bw = TRUE, na.rm = TRUE)
plot(nbcomb22_award_plot_binning,theme.bw = TRUE)

## Panel (D) IGF
nbcomb32_award_plot_binning <- interflex(Y = "num_bids", D = "connectedbin", X = "igf", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[df_nog$igf>=.22 & df_nog$igf<=.4,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 4)", theme.bw = TRUE, na.rm = TRUE)
plot(nbcomb32_award_plot_binning,theme.bw = TRUE)


# Figure B8: Linear Interaction Diagnostics Plots - Competition and Alternative Bureaucrats’ Capabilities Measures

## DV: Award as Single Bidder

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Panel (A) Alt. Capabilities Index
rcomb02 <- interflex(estimator = "raw", Y = "risky_award", D = "connectedbin", X = "b_capacity_alt", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 1)", theme.bw = TRUE)
plot(rcomb02,theme.bw = TRUE)

## Panel (B) IGA
rcomb12 <- interflex(estimator = "raw", Y = "risky_award", D = "connectedbin", X = "iga", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 2)", theme.bw = TRUE)
plot(rcomb12,theme.bw = TRUE)

## Panel (C) IGE
rcomb22 <- interflex(estimator = "raw", Y = "risky_award", D = "connectedbin", X = "ige", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 3)", theme.bw = TRUE)
plot(rcomb22,theme.bw = TRUE)

## Panel (D) IGF
rcomb32 <- interflex(estimator = "raw", Y = "risky_award", D = "connectedbin", X = "igf", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt.4)", theme.bw = TRUE)
plot(rcomb32,theme.bw = TRUE)


## DV: Number of Bids

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Panel (A) Alt. Capabilities Index
nbcomb02 <- interflex(estimator = "raw", Y = "num_bids", D = "connectedbin", X = "b_capacity_alt", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 1)", theme.bw = TRUE)
plot(nbcomb02,theme.bw = TRUE)

## Panel (B) IGA
nbcomb12 <- interflex(estimator = "raw", Y = "num_bids", D = "connectedbin", X = "iga", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 2)", theme.bw = TRUE)
plot(nbcomb12,theme.bw = TRUE)

## Panel (C) IGE
nbcomb22 <- interflex(estimator = "raw", Y = "num_bids", D = "connectedbin", X = "ige", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt. 3)", theme.bw = TRUE)
plot(nbcomb22,theme.bw = TRUE)

## Panel (D) IGF
nbcomb32 <- interflex(estimator = "raw", Y = "num_bids", D = "connectedbin", X = "igf", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bidder", Dlabel = "Connection", Xlabel="Bureaucrat's Capabilities (Alt.4)", theme.bw = TRUE)
plot(nbcomb32,theme.bw = TRUE)



# Table B11: Political Connections, Bureaucrats’ Capabilities and Awards to Single Bidders (Alternative Measures of Bureaucrats’ Capabilities)

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Linear Probability Models
## Model_0: Model with controls and FEs; Main connections (binary) + BCI using IGA + IGE + IGF
racon2_lm0 <- lm(risky_award ~ b_capacity_alt + connectedbin + b_capacity_alt:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$b_capacity_alt<=0.52,])
racon2_lm0$vcov <- vcovCL(racon2_lm0, cluster = ~ entity_code + year + nit_bidder)
racon2_lm0_coef = coeftest(racon2_lm0, racon2_lm0$vcov)
racon2_lm0_cse <- sqrt(diag(racon2_lm0$vcov))

## Model 1: Model with controls and FEs; Main connections (binary) + BCI using IGA 
racon2_lm1 <- lm(risky_award ~ iga + connectedbin + iga:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$iga<=.8,])
racon2_lm1$vcov <- vcovCL(racon2_lm1, cluster = ~ entity_code + year + nit_bidder)
racon2_lm1_coef = coeftest(racon2_lm1, racon2_lm1$vcov)
racon2_lm1_cse <- sqrt(diag(racon2_lm1$vcov))

## Model 2: Model with controls and FEs; Main connections (binary) + BCI using IGE 
racon2_lm2 <- lm(risky_award ~ ige + connectedbin + ige:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$ige<=.50,])
racon2_lm2$vcov <- vcovCL(racon2_lm2, cluster = ~ entity_code + year + nit_bidder)
racon2_lm2_coef = coeftest(racon2_lm2, racon2_lm2$vcov)
racon2_lm2_cse <- sqrt(diag(racon2_lm2$vcov))

## Model 3: Model with controls and FEs; Main connections (binary) + BCI using IGF
racon2_lm3 <- lm(risky_award ~ igf + connectedbin + igf:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$igf>=.22 & df_nog$igf<=.4,])
racon2_lm3$vcov <- vcovCL(racon2_lm3, cluster = ~ entity_code + year + nit_bidder)
racon2_lm3_coef = coeftest(racon2_lm3, racon2_lm3$vcov)
racon2_lm3_cse <- sqrt(diag(racon2_lm3$vcov))

## Logistics Regressions
## Model_0: Model with controls and FEs; Main connections (binary) + BCI using IGA + IGE + IGF
racon2_0 <- glm(risky_award ~ b_capacity_alt + connectedbin + b_capacity_alt:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$b_capacity_alt<=0.52,], family = binomial)
racon2_0$vcov <- vcovCL(racon2_0, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
racon2_0_coef = coeftest(racon2_0, racon2_0$vcov)
racon2_0_cse <- sqrt(diag(racon2_0$vcov))

## Model 1: Model with controls and FEs; Main connections (binary) + BCI using IGA 
racon2_1 <- glm(risky_award ~ iga + connectedbin + iga:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$iga<=.8,], family = binomial)
racon2_1$vcov <- vcovCL(racon2_1, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
racon2_1_coef = coeftest(racon2_1, racon2_1$vcov)
racon2_1_cse <- sqrt(diag(racon2_1$vcov))

## Model 2: Model with controls and FEs; Main connections (binary) + BCI using IGE 
racon2_2 <- glm(risky_award ~ ige + connectedbin + ige:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$ige<=.50,], family = binomial)
racon2_2$vcov <- vcovCL(racon2_2, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
racon2_2_coef = coeftest(racon2_2, racon2_2$vcov)
racon2_2_cse <- sqrt(diag(racon2_2$vcov))

## Model 3: Model with controls and FEs; Main connections (binary) + BCI using IGF
racon2_3 <- glm(risky_award ~ igf + connectedbin + igf:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$igf>=.22 & df_nog$igf<=.4,], family = binomial)
racon2_3$vcov <- vcovCL(racon2_3, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
racon2_3_coef = coeftest(racon2_3, racon2_3$vcov)
racon2_3_cse <- sqrt(diag(racon2_3$vcov))

# TABLE
stargazer(racon2_lm0, racon2_0, racon2_lm1, racon2_1, racon2_lm2, racon2_2, racon2_lm3, racon2_3, se = list(racon2_lm0_cse, racon2_0_cse, racon2_lm1_cse, racon2_1_cse, racon2_lm2_cse, racon2_2_cse, racon2_lm3_cse, racon2_3_cse), 
          title="Connections, Bureaucrats' Capabilities and Awards to Single Bidders (Alternatie Measures of Bureaucrats' Capabilities)",
          align=TRUE, dep.var.labels=c("risky_award"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"), 
          no.space=TRUE, add.lines = list(c("Controls", "Y","Y","Y","Y","Y","Y","Y","Y"),
                                          c("Municipality FE", "Y","Y","Y","Y","Y","Y","Y","Y"),
                                          c("Year FE","Y","Y","Y","Y","Y","Y","Y","Y")),  keep = c("Constant","connectedbin","b_capacity_alt","iga","ige","igf"))


# Table B12: Political Connections, Bureaucrats’ Capabilities and the Number of Bids (Alternative Measures of Bureaucrats’ Capabilities)

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Linear Probability Models
## Model_0: Model with controls and FEs; Main connections (binary) + BCI using IGA + IGE + IGF
nbacon2_lm0 <- lm(num_bids ~ b_capacity_alt + connectedbin + b_capacity_alt:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$b_capacity_alt<=0.52,])
nbacon2_lm0$vcov <- vcovCL(nbacon2_lm0, cluster = ~ entity_code + year + nit_bidder)
nbacon2_lm0_coef <- coeftest(nbacon2_lm0, nbacon2_lm0$vcov)
nbacon2_lm0_cse <- sqrt(diag(nbacon2_lm0$vcov))

## Model 1: Model with controls and FEs; Main connections (binary) + BCI using IGA 
nbacon2_lm1 <- lm(num_bids ~ iga + connectedbin + iga:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$iga<=.8,])
nbacon2_lm1$vcov <- vcovCL(nbacon2_lm1, cluster = ~ entity_code + year + nit_bidder)
nbacon2_lm1_coef <- coeftest(nbacon2_lm1, nbacon2_lm1$vcov)
nbacon2_lm1_cse <- sqrt(diag(nbacon2_lm1$vcov))

## Model 2: Model with controls and FEs; Main connections (binary) + BCI using IGE 
nbacon2_lm2 <- lm(num_bids ~ ige + connectedbin + ige:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$ige<=.50,])
nbacon2_lm2$vcov <- vcovCL(nbacon2_lm2, cluster = ~ entity_code + year + nit_bidder)
nbacon2_lm2_coef <- coeftest(nbacon2_lm2, nbacon2_lm2$vcov)
nbacon2_lm2_cse <- sqrt(diag(nbacon2_lm2$vcov))

## Model 3: Model with controls and FEs; Main connections (binary) + BCI using IGF
nbacon2_lm3 <- lm(num_bids ~ igf + connectedbin + igf:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$igf>=.22 & df_nog$igf<=.4,])
nbacon2_lm3$vcov <- vcovCL(nbacon2_lm3, cluster = ~ entity_code + year + nit_bidder)
nbacon2_lm3_coef <- coeftest(nbacon2_lm3, nbacon2_lm3$vcov)
nbacon2_lm3_cse <- sqrt(diag(nbacon2_lm3$vcov))

## TABLE
stargazer(nbacon2_lm0, nbacon2_lm1, nbacon2_lm2, nbacon2_lm3, 
          se = list(nbacon2_lm0_cse, nbacon2_lm1_cse, nbacon2_lm2_cse, nbacon2_lm3_cse), 
          title="Political Connections, Bureaucrats' Capabilities and the Number of Bids (Alternatie Measures of Bureaucrats' Capabilities)",
          align=TRUE, dep.var.labels=c("risky_award"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"), 
          no.space=TRUE, add.lines = list(c("Controls", "Y","Y","Y","Y"),
                                          c("Municipality FE", "Y","Y","Y","Y"),
                                          c("Year FE","Y","Y","Y","Y")),  keep = c("Constant","connectedbin","b_capacity_alt","iga","ige","igf"))



# Figure B9: Political Connections, Index of Municipal Management, Procurement Awards and Competition (Binning Estimator)

## Panel (A) DV: award; Interaction: Main connections (binary), BCI using IGM
comb42_award_plot_binning <- interflex(Y = "award", D = "connectedbin", X = "igm", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df[df$igm>=.18 & df$igm<=.5,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Index of Municipal Management", theme.bw = TRUE, na.rm = TRUE)
plot(comb42_award_plot_binning,theme.bw = TRUE)

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Panel (B) DV: award to sole bidder; Interaction: Main connections (binary), BCI usingIGM
rcomb42_award_plot_binning <- interflex(Y = "risky_award", D = "connectedbin", X = "igm", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[df_nog$igm>=.18 & df_nog$igm<=.5,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Index of Municipal Management", theme.bw = TRUE, na.rm = TRUE)
plot(rcomb42_award_plot_binning,theme.bw = TRUE)

## Panel (C) DV: number of competing bids; Interaction: Main connections (binary), BCI usingIGM
nbcomb42_award_plot_binning <- interflex(Y = "num_bids", D = "connectedbin", X = "igm", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[df_nog$igm>=.18 & df_nog$igm<=.5,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bidder", Dlabel = "Connection", Xlabel="Index of Municipal Management", theme.bw = TRUE, na.rm = TRUE)
plot(nbcomb42_award_plot_binning,theme.bw = TRUE)



# Figure B10: Linear Interaction Diagnostics Plots - Procurement Contracts, Competition, and the Index of Municipal Management

## Panel (A) DV: award; Interaction: Main connections (binary), BCI using IGM
comb42 <- interflex(estimator = "raw", Y = "award", D = "connectedbin", X = "igm", ncols=2, data = df, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection", Xlabel="Index of Municipal Management", theme.bw = TRUE)
plot(comb42,theme.bw = TRUE)

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Panel (B) DV: award to sole bidder; Interaction: Main connections (binary), BCI using IGM
rcomb42 <- interflex(estimator = "raw", Y = "risky_award", D = "connectedbin", X = "igm", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection", Xlabel="Index of Municipal Management", theme.bw = TRUE)
plot(rcomb42,theme.bw = TRUE)

## Panel (C) DV: number of competing bids; Interaction: Main connections (binary), BCI using IGM
nbcomb42 <- interflex(estimator = "raw", Y = "num_bids", D = "connectedbin", X = "igm", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "No. of Bidder", Dlabel = "Connection", Xlabel="Index of Municipal Management", theme.bw = TRUE)
plot(nbcomb42,theme.bw = TRUE)



# Table B13: Political Connections, Index of Municipal Management, Procurement Contracts and Competition

## Linear probability; DV: award; Interaction: Main connections (binary), BCI using IGM
acon2_lm4 <- lm(award ~ igm + connectedbin + igf:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[df$igm>=.18 & df$igm<=.5,])
acon2_lm4$vcov <- vcovCL(acon2_lm4, cluster = ~ entity_code + year + nit_bidder)
acon2_lm4_coef = coeftest(acon2_lm4, acon2_lm4$vcov)
acon2_lm4_cse <- sqrt(diag(acon2_lm4$vcov))

## Logistic regression; DV: award; Interaction: Main connections (binary), BCI using IGM
acon2_4 <- glm(award ~ igm + connectedbin + igf:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[df$igm>=.18 & df$igm<=.5,], family = binomial)
acon2_4$vcov <- vcovCL(acon2_4, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
acon2_4_coef = coeftest(acon2_4, acon2_4$vcov)
acon2_4_cse <- sqrt(diag(acon2_4$vcov))

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Linear probability; DV: award to sole bidder; Interaction: Main connections (binary), BCI using IGM
racon2_lm4 <- lm(risky_award ~ igm + connectedbin + igf:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$igm>=.18 & df_nog$igm<=.5,])
racon2_lm4$vcov <- vcovCL(racon2_lm4, cluster = ~ entity_code + year + nit_bidder)
racon2_lm4_coef = coeftest(racon2_lm4, racon2_lm4$vcov)
racon2_lm4_cse <- sqrt(diag(racon2_lm4$vcov))

## Logistic regression; DV: award to sole bidder; Interaction: Main connections (binary), BCI using IGM
racon2_4 <- glm(risky_award ~ igm + connectedbin + igf:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$igm>=.18 & df_nog$igm<=.5,], family = binomial)
racon2_4$vcov <- vcovCL(racon2_4, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
racon2_4_coef = coeftest(racon2_4, racon2_4$vcov)
racon2_4_cse <- sqrt(diag(racon2_4$vcov))

## Linear regression; DV: number of bidders; Interaction: Main connections (binary), BCI using IGM
nbacon2_lm4 <- lm(num_bids ~ igm + connectedbin + igf:connectedbin + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[df_nog$igm>=.18 & df_nog$igm<=.5,])
nbacon2_lm4$vcov <- vcovCL(nbacon2_lm4, cluster = ~ entity_code + year + nit_bidder)
nbacon2_lm4_coef <- coeftest(nbacon2_lm4, nbacon2_lm4$vcov)
nbacon2_lm4_cse <- sqrt(diag(nbacon2_lm4$vcov))


#-> TABLE: IGM Results
stargazer(acon2_lm4, acon2_4, racon2_lm4, racon2_4, nbacon2_lm4, 
          se = list(acon2_lm4_cse, acon2_4_cse, racon2_lm4_cse, racon2_4_cse, nbacon2_lm4_cse), 
          title="Political Connections, Index of Municipal Management, Procurement Contracts and Competition",
          align=TRUE, dep.var.labels=c("risky_award"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"), 
          no.space=TRUE, add.lines = list(c("Controls", "Y","Y","Y","Y","Y"),
                                          c("Municipality FE", "Y","Y","Y","Y","Y"),
                                          c("Year FE","Y","Y","Y","Y","Y")),  keep = c("Constant","connectedbin","b_capacity_alt","igm"))



###########################################
##  Tables and Figures in Appendix C     ## 
###########################################

# Figure C1: Networks of Incumbent Mayors in 2012-2015 and 2016-2019

library(igraph) # For Network Visualization

## 2012-2015 Network  

## Load the 2011 Candidate Pairs dataset
mayors <- read_csv("PoliticalNetworksData_2011.csv")

## Preparing the data ploting graphs

## Keep only mayors (i.e., those who won elections in 2011)
mayors <- mayors[mayors$elected_a == 1 & mayors$elected_b == 1,]

## List of unique parties
uniqueparty = unique(mayors$party_a_2011)
uniquemayor = unique(mayors$name_candidate_a)

## Lists of nodes
candidate_a <- mayors %>%
  distinct(name_candidate_a) %>%
  #rename(name_candidate_a = label)
  rename(label = name_candidate_a)

candidate_b <- mayors %>%
  distinct(name_candidate_b) %>%
  #rename(name_candidate_b = label)
  rename(label = name_candidate_b)

nodes_0 <- full_join(candidate_a, candidate_b, by = "label")
nodes_0 <- nodes_0 %>% rowid_to_column("id")

## Adding political party to the list of nodes
attributes = mayors[!duplicated(mayors$name_candidate_a),]
attributes = subset(attributes, select = c(party_a_2011,name_candidate_a))
colnames(attributes)[2] <- "label"
nodes <-merge(x=nodes_0,y=attributes,by="label",all.x=TRUE,all.y=FALSE)

## List of edges
keep <- c("name_candidate_a", "name_candidate_b", "connected")
edges <- mayors[keep]
edges <- edges[edges$connected == 1,]

edges1 <- edges %>% 
  left_join(nodes, by = c("name_candidate_a" = "label")) %>% 
  #rename(id = id_a)
  rename(id_a = id)

edges1 <- edges1 %>% 
  left_join(nodes, by = c("name_candidate_b" = "label")) %>% 
  #rename(id = id_b)
  rename(id_b = id)

edges2 <- subset(edges1, select = c(id_a, id_b, connected) )

## Creating a Graph Object
pairs_igraph2011 <- graph_from_data_frame(d = edges2, vertices = nodes_0, directed = FALSE)
pairs_igraph2011

## Panel (A) Graph of the Network 2012-2015
coords <- layout_(pairs_igraph2011, with_mds())
plot(pairs_igraph2011, layout = coords*0.5, edge.arrow.size = 0.5, 
     vertex.color= "blue", vertex.size=2, vertex.label.cex=0.2)

## Dropping Unconnected Mayors
Isolated = which(degree(pairs_igraph2011)==0)
pairs_igraph2011_conn = delete.vertices(pairs_igraph2011, Isolated)
clust <- cluster_optimal(pairs_igraph2011_conn)
coords2 = coords[-Isolated,]

## Panel (B) Clusters with more than 1 node 2012-2015
plot(clust, pairs_igraph2011_conn, layout=coords2, edge.arrow.size = 0.5,vertex.size=3,vertex.label.cex=0.1)


## 2016-2019 Network  

## Load the 2015 Candidate Pairs dataset
mayors <- read_csv("PoliticalNetworksData_2015.csv")

## Preparing the Data for Plotting Graphs

## Keep only mayors (i.e., those who won elections in 2015)
mayors <- mayors[mayors$elected_a == 1 & mayors$elected_b == 1,]

## List of unique parties
uniqueparty = unique(mayors$party_a_2015)
uniquemayor = unique(mayors$name_candidate_a)

## Lists of nodes
candidate_a <- mayors %>%
  distinct(name_candidate_a) %>%
  #rename(name_candidate_a = label)
  rename(label = name_candidate_a)

candidate_b <- mayors %>%
  distinct(name_candidate_b) %>%
  #rename(name_candidate_b = label)
  rename(label = name_candidate_b)

nodes_0 <- full_join(candidate_a, candidate_b, by = "label")
nodes_0 <- nodes_0 %>% rowid_to_column("id")
nodes_0

## Adding political party to the list of nodes
attributes = mayors[!duplicated(mayors$name_candidate_a),]
attributes = subset(attributes, select = c(party_a_2015,name_candidate_a))
colnames(attributes)[2] <- "label"
nodes <-merge(x=nodes_0,y=attributes,by="label",all.x=TRUE,all.y=FALSE)

## List of edges
keep <- c("name_candidate_a", "name_candidate_b", "connected")
edges <- mayors[keep]
edges <- edges[edges$connected == 1,]

edges1 <- edges %>% 
  left_join(nodes, by = c("name_candidate_a" = "label")) %>% 
  #rename(id = id_a)
  rename(id_a = id)

edges1 <- edges1 %>% 
  left_join(nodes, by = c("name_candidate_b" = "label")) %>% 
  #rename(id = id_b)
  rename(id_b = id)

edges2 <- subset(edges1, select = c(id_a, id_b, connected) )

## Creating a Graph Object
pairs_igraph2015 <- graph_from_data_frame(d = edges2, vertices = nodes_0, directed = FALSE)
pairs_igraph2015

## Panel (C) Graph of the Network 2016-2019
coords <- layout_(pairs_igraph2015, with_mds())
plot(pairs_igraph2015, layout = coords*0.5, edge.arrow.size = 0.5, 
     vertex.color= "blue", vertex.size=2, vertex.label.cex=0.2)

## Dropping Unconnected Mayors
Isolated = which(degree(pairs_igraph2015)==0)
pairs_igraph2015_conn = delete.vertices(pairs_igraph2015, Isolated)
clust <- cluster_optimal(pairs_igraph2015_conn)

coords2 = coords[-Isolated,]

## Panel (D) Clusters with more than 1 node 2012-2015
plot(clust, pairs_igraph2015_conn, layout=coords2, edge.arrow.size = 0.5,vertex.size=3,vertex.label.cex=0.1)



# Table C1: Comparison Between Connected and Unconnected Bidders

## Additional variables
df <- df %>%
  mutate(avg_valcontracts_munisam = case_when(valcontracts_munisam > 0 ~ valcontracts_munisam/numcontracts_munisam,
                                              valcontracts_munisam == 0 ~ NA) ) %>%
  mutate(avg_valcontracts_muninei = case_when(valcontracts_muninei > 0 ~ valcontracts_muninei/numcontracts_muninei,
                                              valcontracts_muninei == 0 ~ NA) ) %>%
  mutate(avg_valcontracts_gov = case_when(valcontracts_gov > 0 ~ valcontracts_gov/numcontracts_gov,
                                          numcontracts_gov == 0 ~ NA) )

## Subsets
df_ycon <- df[which(df$connectedbin==1),]
df_ncon <- df[which(df$connectedbin==0),]

## Number of Contracts with the Central Government
mean(df_ncon$numcontracts_gov,na.rm=TRUE) -mean(df_ycon$numcontracts_gov,na.rm =TRUE)
t.test(x = df_ncon$numcontracts_gov, y = df_ycon$numcontracts_gov, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts with Neighboring Municipalities
mean(df_ncon$numcontracts_muninei,na.rm=TRUE) -mean(df_ycon$numcontracts_muninei,na.rm =TRUE)
t.test(x = df_ncon$numcontracts_muninei, y = df_ycon$numcontracts_muninei, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts with the Same Municipality
mean(df_ncon$numcontracts_munisam,na.rm=TRUE) -mean(df_ycon$numcontracts_munisam,na.rm =TRUE)
t.test(x = df_ncon$numcontracts_munisam, y = df_ycon$numcontracts_munisam, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Average value of Contracts with the Central Government
mean(df_ncon$avg_valcontracts_gov,na.rm=TRUE) -mean(df_ycon$avg_valcontracts_gov,na.rm =TRUE)
t.test(x = df_ncon$avg_valcontracts_gov, y = df_ycon$avg_valcontracts_gov, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Average value of Contracts with Neighboring Municipalities
mean(df_ncon$avg_valcontracts_muninei,na.rm=TRUE) -mean(df_ycon$avg_valcontracts_muninei,na.rm =TRUE)
t.test(x = df_ncon$avg_valcontracts_muninei, y = df_ycon$avg_valcontracts_muninei, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Average value of Contracts with the Same Municipality
mean(df_ncon$avg_valcontracts_munisam,na.rm=TRUE) -mean(df_ycon$avg_valcontracts_munisam,na.rm =TRUE)
t.test(x = df_ncon$avg_valcontracts_munisam, y = df_ycon$avg_valcontracts_munisam, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts Through Cotización with Neighboring Municipalities 
mean(df_ncon$numcontracts_muninei_ct, na.rm=TRUE) -mean(df_ycon$numcontracts_muninei_ct, na.rm =TRUE)
t.test(x = df_ncon$numcontracts_muninei_ct, y = df_ycon$numcontracts_muninei_ct, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts Through Cotización with Same Municipality
mean(df_ncon$numcontracts_munisam_ct, na.rm=TRUE) -mean(df_ycon$numcontracts_munisam_ct, na.rm =TRUE)
t.test(x = df_ncon$numcontracts_munisam_ct, y = df_ycon$numcontracts_munisam_ct, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts Through Licitación with Neighboring Municipalities 
mean(df_ncon$numcontracts_muninei_lt, na.rm=TRUE) -mean(df_ycon$numcontracts_muninei_lt, na.rm =TRUE)
t.test(x = df_ncon$numcontracts_muninei_lt, y = df_ycon$numcontracts_muninei_lt, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts Through Licitación with Same Municipality
mean(df_ncon$numcontracts_munisam_lt, na.rm=TRUE) -mean(df_ycon$numcontracts_munisam_lt, na.rm =TRUE)
t.test(x = df_ncon$numcontracts_munisam_lt, y = df_ycon$numcontracts_munisam_lt, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of years as bidder
mean(df_ncon$firm_procage, na.rm=TRUE) -mean(df_ycon$firm_procage, na.rm =TRUE)
t.test(x = df_ncon$firm_procage, y = df_ycon$firm_procage, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Age of the firm 
mean(df_ncon$firm_age, na.rm=TRUE) -mean(df_ycon$firm_age, na.rm =TRUE)
t.test(x = df_ncon$firm_age, y = df_ycon$firm_age, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)



# Table C2: Comparison Between Connected and Unconnected Bidders (Alternative Connections Measures)

## Alternative Connections Measure: shared of contracts  

## Subsets
df_ycon <- df[which(df$connectedbin_sharenumc==1),]
df_ncon <- df[which(df$connectedbin_sharenumc==0),]

## Number of Contracts with the Central Government
mean(df_ncon$numcontracts_gov,na.rm=TRUE) -mean(df_ycon$numcontracts_gov,na.rm =TRUE)
t.test(x = df_ncon$numcontracts_gov, y = df_ycon$numcontracts_gov, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts with Neighboring Municipalities
mean(df_ncon$numcontracts_muninei,na.rm=TRUE) -mean(df_ycon$numcontracts_muninei,na.rm =TRUE)
t.test(x = df_ncon$numcontracts_muninei, y = df_ycon$numcontracts_muninei, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts with the Same Municipality
mean(df_ncon$numcontracts_munisam,na.rm=TRUE) -mean(df_ycon$numcontracts_munisam,na.rm =TRUE)
t.test(x = df_ncon$numcontracts_munisam, y = df_ycon$numcontracts_munisam, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Average value of Contracts with the Central Government
mean(df_ncon$avg_valcontracts_gov,na.rm=TRUE) -mean(df_ycon$avg_valcontracts_gov,na.rm =TRUE)
t.test(x = df_ncon$avg_valcontracts_gov, y = df_ycon$avg_valcontracts_gov, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Average value of Contracts with Neighboring Municipalities
mean(df_ncon$avg_valcontracts_muninei,na.rm=TRUE) -mean(df_ycon$avg_valcontracts_muninei,na.rm =TRUE)
t.test(x = df_ncon$avg_valcontracts_muninei, y = df_ycon$avg_valcontracts_muninei, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Average value of Contracts with the Same Municipality
mean(df_ncon$avg_valcontracts_munisam,na.rm=TRUE) -mean(df_ycon$avg_valcontracts_munisam,na.rm =TRUE)
t.test(x = df_ncon$avg_valcontracts_munisam, y = df_ycon$avg_valcontracts_munisam, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts Through Cotización with Neighboring Municipalities 
mean(df_ncon$numcontracts_muninei_ct, na.rm=TRUE) -mean(df_ycon$numcontracts_muninei_ct, na.rm =TRUE)
t.test(x = df_ncon$numcontracts_muninei_ct, y = df_ycon$numcontracts_muninei_ct, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts Through Cotización with Same Municipality
mean(df_ncon$numcontracts_munisam_ct, na.rm=TRUE) -mean(df_ycon$numcontracts_munisam_ct, na.rm =TRUE)
t.test(x = df_ncon$numcontracts_munisam_ct, y = df_ycon$numcontracts_munisam_ct, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts Through Licitación with Neighboring Municipalities 
mean(df_ncon$numcontracts_muninei_lt, na.rm=TRUE) -mean(df_ycon$numcontracts_muninei_lt, na.rm =TRUE)
t.test(x = df_ncon$numcontracts_muninei_lt, y = df_ycon$numcontracts_muninei_lt, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts Through Licitación with Same Municipality
mean(df_ncon$numcontracts_munisam_lt, na.rm=TRUE) -mean(df_ycon$numcontracts_munisam_lt, na.rm =TRUE)
t.test(x = df_ncon$numcontracts_munisam_lt, y = df_ycon$numcontracts_munisam_lt, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of years as bidder
mean(df_ncon$firm_procage, na.rm=TRUE) -mean(df_ycon$firm_procage, na.rm =TRUE)
t.test(x = df_ncon$firm_procage, y = df_ycon$firm_procage, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Age of the firm 
mean(df_ncon$firm_age, na.rm=TRUE) -mean(df_ycon$firm_age, na.rm =TRUE)
t.test(x = df_ncon$firm_age, y = df_ycon$firm_age, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Alternative Connections Measure: shared of value of contracts  
df_ycon <- df[which(df$connectedbin_sharevalc==1),]
df_ncon <- df[which(df$connectedbin_sharevalc==0),]

## Number of Contracts with the Central Government
mean(df_ncon$numcontracts_gov,na.rm=TRUE) -mean(df_ycon$numcontracts_gov,na.rm =TRUE)
t.test(x = df_ncon$numcontracts_gov, y = df_ycon$numcontracts_gov, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts with Neighboring Municipalities
mean(df_ncon$numcontracts_muninei,na.rm=TRUE) -mean(df_ycon$numcontracts_muninei,na.rm =TRUE)
t.test(x = df_ncon$numcontracts_muninei, y = df_ycon$numcontracts_muninei, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts with the Same Municipality
mean(df_ncon$numcontracts_munisam,na.rm=TRUE) -mean(df_ycon$numcontracts_munisam,na.rm =TRUE)
t.test(x = df_ncon$numcontracts_munisam, y = df_ycon$numcontracts_munisam, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Average value of Contracts with the Central Government
mean(df_ncon$avg_valcontracts_gov,na.rm=TRUE) -mean(df_ycon$avg_valcontracts_gov,na.rm =TRUE)
t.test(x = df_ncon$avg_valcontracts_gov, y = df_ycon$avg_valcontracts_gov, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Average value of Contracts with Neighboring Municipalities
mean(df_ncon$avg_valcontracts_muninei,na.rm=TRUE) -mean(df_ycon$avg_valcontracts_muninei,na.rm =TRUE)
t.test(x = df_ncon$avg_valcontracts_muninei, y = df_ycon$avg_valcontracts_muninei, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Average value of Contracts with the Same Municipality
mean(df_ncon$avg_valcontracts_munisam,na.rm=TRUE) -mean(df_ycon$avg_valcontracts_munisam,na.rm =TRUE)
t.test(x = df_ncon$avg_valcontracts_munisam, y = df_ycon$avg_valcontracts_munisam, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts Through Cotización with Neighboring Municipalities 
mean(df_ncon$numcontracts_muninei_ct, na.rm=TRUE) -mean(df_ycon$numcontracts_muninei_ct, na.rm =TRUE)
t.test(x = df_ncon$numcontracts_muninei_ct, y = df_ycon$numcontracts_muninei_ct, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts Through Cotización with Same Municipality
mean(df_ncon$numcontracts_munisam_ct, na.rm=TRUE) -mean(df_ycon$numcontracts_munisam_ct, na.rm =TRUE)
t.test(x = df_ncon$numcontracts_munisam_ct, y = df_ycon$numcontracts_munisam_ct, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts Through Licitación with Neighboring Municipalities 
mean(df_ncon$numcontracts_muninei_lt, na.rm=TRUE) -mean(df_ycon$numcontracts_muninei_lt, na.rm =TRUE)
t.test(x = df_ncon$numcontracts_muninei_lt, y = df_ycon$numcontracts_muninei_lt, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of Contracts Through Licitación with Same Municipality
mean(df_ncon$numcontracts_munisam_lt, na.rm=TRUE) -mean(df_ycon$numcontracts_munisam_lt, na.rm =TRUE)
t.test(x = df_ncon$numcontracts_munisam_lt, y = df_ycon$numcontracts_munisam_lt, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Number of years as bidder
mean(df_ncon$firm_procage, na.rm=TRUE) -mean(df_ycon$firm_procage, na.rm =TRUE)
t.test(x = df_ncon$firm_procage, y = df_ycon$firm_procage, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)

## Age of the firm 
mean(df_ncon$firm_age, na.rm=TRUE) -mean(df_ycon$firm_age, na.rm =TRUE)
t.test(x = df_ncon$firm_age, y = df_ycon$firm_age, alternative = c("two.sided", "less", "greater"), mu = 0, 
       paired = FALSE, var.equal = FALSE, conf.level = 0.95)



############################################
## Close Elections RDD                    ##
############################################
library(rdrobust) 

# Import data: firm-muni-year
df_rdd_bids <- read.csv("gtm_RDD_connectedfirmsyears.csv", stringsAsFactors = FALSE)

# How many mayors sought re-election? how many were re-elected?
df_muni0<- df_rdd_bids %>%
  group_by(entity_code) %>%
  summarise_at(vars(ran_for_reelection), list(ran_for_reelection = max)) 

summary(df_muni0$ran_for_reelection)

# Keep only mayors that ran for re-election in 2015
df_rdd_bids<- filter(df_rdd_bids, ran_for_reelection == 1)

# How many of the mayors who sought re-election actually got it?
df_muni0<- df_rdd_bids %>%
  group_by(entity_code) %>%
  summarise_at(vars(reelected), list(reelected = max)) 

summary(df_muni0$reelected)


# Table C3: Close Elections RDD - Connections and Number of Contracts per Year

## Column 1
## bw method: msesum; reported estimate: robust
summary(rdrobust(y=df_rdd_bids$num_contracts, x=df_rdd_bids$margin, all=TRUE, bwselect = "msesum", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )

## Column 2
## bw method: mserd; reported estimate: robust
summary(rdrobust(y=df_rdd_bids$num_contracts, x=df_rdd_bids$margin, all=TRUE, bwselect = "mserd", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )

## Column 3
## bw method: msetwo; reported estimate: robust
summary(rdrobust(y=df_rdd_bids$num_contracts, x=df_rdd_bids$margin, all=TRUE, bwselect = "msetwo", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )

## Column 4
## bw method: cerrd; reported estimate: robust
summary(rdrobust(y=df_rdd_bids$num_contracts, x=df_rdd_bids$margin, all=TRUE, bwselect = "cerrd", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )



# Table C4: Close Elections RDD - Connections and Value of Contracts per Year

## Column 1
## bw method: msetwo; reported estimate: robust
summary(rdrobust(y=log(df_rdd_bids$val_contracts+1), x=df_rdd_bids$margin, all=TRUE, bwselect = "msetwo", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )

## Column 2
## bw method: msesum; reported estimate: robust
summary(rdrobust(y=log(df_rdd_bids$val_contracts+1), x=df_rdd_bids$margin, all=TRUE, bwselect = "msesum", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )

## Column 3
## bw method: mserd; reported estimate: robust
summary(rdrobust(y=log(df_rdd_bids$val_contracts+1), x=df_rdd_bids$margin, all=TRUE, bwselect = "mserd", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )

## Column 4
## bw method: cerrd; reported estimate: robust
summary(rdrobust(y=log(df_rdd_bids$val_contracts+1), x=df_rdd_bids$margin, all=TRUE, bwselect = "cerrd", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )



# Table C5: Close Elections RDD - Connections and Number of Contracts per Year as Single Bidder

## Column 1
## bw method: msetwo; reported estimate: robust
summary(rdrobust(y=df_rdd_bids$num_sb_contracts, x=df_rdd_bids$margin, all=TRUE, bwselect = "msetwo", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )

## Column 2
## bw method: mserd; reported estimate: robust
summary(rdrobust(y=df_rdd_bids$num_sb_contracts, x=df_rdd_bids$margin, all=TRUE, bwselect = "mserd", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )

## Column 3
## bw method: msesum; reported estimate: robust
summary(rdrobust(y=df_rdd_bids$num_sb_contracts, x=df_rdd_bids$margin, all=TRUE, bwselect = "msesum", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )

## Column 4
## bw method: cerrd; reported estimate: robust
summary(rdrobust(y=df_rdd_bids$num_sb_contracts, x=df_rdd_bids$margin, all=TRUE, bwselect = "cerrd", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )



# Table C6: Close Elections RDD - Connections and Value of Contracts per Year as Single Bidder

## Column 1
## bw method: msesum; reported estimate: robust
summary(rdrobust(y=log(df_rdd_bids$val_sb_contracts+1), x=df_rdd_bids$margin, all=TRUE, bwselect = "msesum", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )

## Column 2
## bw method: msetwo; reported estimate: robust
summary(rdrobust(y=log(df_rdd_bids$val_sb_contracts+1), x=df_rdd_bids$margin, all=TRUE, bwselect = "msetwo", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )

## Column 3
## bw method: mserd; reported estimate: robust
summary(rdrobust(y=log(df_rdd_bids$val_sb_contracts+1), x=df_rdd_bids$margin, all=TRUE, bwselect = "mserd", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )

## Column 4
## bw method: cerrd; reported estimate: robust
summary(rdrobust(y=log(df_rdd_bids$val_sb_contracts+1), x=df_rdd_bids$margin, all=TRUE, bwselect = "cerrd", cluster= as.factor(df_rdd_bids$nit_bidder)  ) )


# Figure C2: Close Elections RDD - Connections and Procurement Contracts

## Number of Contracts
## Panel (A) Local Linear Estimation
lcm1_award <- rdplot(df_rdd_bids$num_contracts, df_rdd_bids$margin, binselect="es", ci=95,
                     p = 1, col.lines = "red", col.dots = "lightgray", 
                     title = "Local Linear Regression", 
                     x.label = "Margin", y.label = "Number of Contracts",
                     y.lim = c(0,0.75))

## Panel (B) Local Polynomial (2nd order)
lcm2_award <- rdplot(df_rdd_bids$num_contracts, df_rdd_bids$margin, binselect="es", ci=95,
                     p = 2, col.lines = "red", col.dots = "lightgray", 
                     title = "Local 2th Order Polynomial Regression", 
                     x.label = "Margin", y.label = "Number of Contracts",
                     y.lim = c(0,0.75))

## Log of Value of Contracts
## Panel (C) Local Linear Estimation
lcm1_value <- rdplot(log(df_rdd_bids$val_contracts+1), df_rdd_bids$margin, binselect="es", ci=95,
                     p = 1, col.lines = "red", col.dots = "lightgray", 
                     title = "Local Linear Regression", 
                     x.label = "Margin", y.label = "Log. of Value of Contracts",
                     y.lim = c(0,3))

## Panel (D) Local Polynomial (2nd order)
lcm2_value <- rdplot(log(df_rdd_bids$val_contracts+1), df_rdd_bids$margin, binselect="es", ci=95,
                     p = 2, col.lines = "red", col.dots = "lightgray", 
                     title = "Local 2th Order Polynomial Regression", 
                     x.label = "Margin", y.label = "Log. of Value of Contracts",
                     y.lim = c(0,3))


# Figure C3: Close Elections RDD - Connections and Procurement Contracts to Single Bidders

## Number of Contracts
## Panel (A) Local Linear Estimation
lcm1_sbaward <- rdplot(df_rdd_bids$num_sb_contracts, df_rdd_bids$margin, binselect="es", ci=95,
                       p = 1, col.lines = "red", col.dots = "lightgray", 
                       title = "Local Linear Regression", 
                       x.label = "Margin", y.label = "Number of Contracts as Single Bidder",
                       y.lim = c(0,0.5))

## Panel (B) Local Polynomial (2nd order)
lcm2_sbaward <- rdplot(df_rdd_bids$num_sb_contracts, df_rdd_bids$margin, binselect="es", ci=95,
                       p = 2, col.lines = "red", col.dots = "lightgray", 
                       title = "Local 2th Order Polynomial Regression", 
                       x.label = "Margin", y.label = "Number of Contracts as Single Bidder",
                       y.lim = c(0,0.5))


## Log of Value of Contracts
## Panel (C) Local Linear Estimation
lcm1_sbvalue <- rdplot(log(df_rdd_bids$val_sb_contracts+1), df_rdd_bids$margin, binselect="es", ci=95,
                       p = 1, col.lines = "red", col.dots = "lightgray", 
                       title = "Local Linear Regression", 
                       x.label = "Margin", y.label = "Log. of Value of Contracts as Single Bidder",
                       y.lim = c(0,3))

## Panel (D) Local Polynomial (2nd order)
lcm2_sbvalue <- rdplot(log(df_rdd_bids$val_sb_contracts+1), df_rdd_bids$margin, binselect="es", ci=95,
                       p = 2, col.lines = "red", col.dots = "lightgray", 
                       title = "Local 2th Order Polynomial Regression", 
                       x.label = "Margin", y.label = "Log. of Value of Contracts as Single Bidder",
                       y.lim = c(0,3))



# Figure C4: Close Elections RDD - Assessment of the Continuity Assumption

## Panel (A) Age of the firm as of 2015
cont1_lpr1 <- rdplot(df_rdd_bids$firm_age, df_rdd_bids$margin, binselect="es", ci=95,
                     p = 1, col.lines = "red", col.dots = "lightgray", 
                     title = "Local Linear Regression", 
                     x.label = "Margin", y.label = "Age of the firm as of 2015",
                     y.lim = c(4,13))

cont1_lpr2 <- rdplot(df_rdd_bids$firm_age, df_rdd_bids$margin, binselect="es", ci=95,
                     p = 2, col.lines = "red", col.dots = "lightgray", 
                     title = "Local 2th Order Polynomial Regression", 
                     x.label = "Margin", y.label = "Age of the firm as of 2015",
                     y.lim = c(0,15))

## Panel (B) Years as Contractor as of 2015
cont2_lpr1 <- rdplot(df_rdd_bids$firm_procage, df_rdd_bids$margin, binselect="es", ci=95,
                     p = 1, col.lines = "red", col.dots = "lightgray", 
                     title = "Local Linear Regression", 
                     x.label = "Margin", y.label = "Years as Contractor as of 2015",
                     y.lim = c(0,5))

cont2_lpr2 <- rdplot(df_rdd_bids$firm_procage, df_rdd_bids$margin, binselect="es", ci=95,
                     p = 2, col.lines = "red", col.dots = "lightgray", 
                     title = "Local 2th Order Polynomial Regression", 
                     x.label = "Margin", y.label = "Years as Contractor as of 2015",
                     y.lim = c(0,5))

## Panel (C) Number of Contracts before 2016
cont3_lpr1 <- rdplot(df_rdd_bids$num_contracts_2015, df_rdd_bids$margin, binselect="es", ci=95,
                     p = 1, col.lines = "red", col.dots = "lightgray", 
                     title = "Local Linear Regression", 
                     x.label = "Margin", y.label = "Number of Contracts Before 2016",
                     y.lim = c(0,14))

cont3_lpr2 <- rdplot(df_rdd_bids$num_contracts_2015, df_rdd_bids$margin, binselect="es", ci=95,
                     p = 2, col.lines = "red", col.dots = "lightgray", 
                     title = "Local 2th Order Polynomial Regression", 
                     x.label = "Margin", y.label = "Number of Contracts Before 2016",
                     y.lim = c(0,14))

## Panel (D) Log of Value of Contracts before 2016
cont4_lpr1 <- rdplot(log(df_rdd_bids$val_contracts_2015+1), df_rdd_bids$margin, binselect="es", ci=95,
                     p = 1, col.lines = "red", col.dots = "lightgray", 
                     title = "Local Linear Regression", 
                     x.label = "Margin", y.label = "Log of Value of Contracts Before 2016",
                     y.lim = c(12,15))

cont4_lpr2 <- rdplot(log(df_rdd_bids$val_contracts_2015+1), df_rdd_bids$margin, binselect="es", ci=95,
                     p = 2, col.lines = "red", col.dots = "lightgray", 
                     title = "Local 2th Order Polynomial Regression", 
                     x.label = "Margin", y.label = "Log of Value of Contracts Before 2016",
                     y.lim = c(12,15))



# Figure C5: Alternative Political Connections, Bureaucrats’ Capabilities, and Procurement Awards (Binning Estimator)

## Panel (A) Average Contract Size
comb1_award_plot_binning <- interflex(Y = "award", D = "rent_usdk", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df[which(df$b_capacity<=0.6,df$rent_usdk<=103.8725),], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection (Alt. 1)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(comb1_award_plot_binning,theme.bw = TRUE)

## Panel (B) Connection 75th
summary(df$rent_usdk)
# Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
# 0.000    1.625   11.800   38.811   55.673 1999.683 

df$connectedbin_75 <- NA
df$connectedbin_75[which(df$rent_usdk <= 55.673 )] <- 0
df$connectedbin_75[which(df$rent_usdk > 55.673 )] <- 1

comb2_award_plot_binning <- interflex(Y = "award", D = "connectedbin_75", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df[df$b_capacity<=0.6,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection (Alt. 2)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(comb2_award_plot_binning,theme.bw = TRUE)

## Panel (C) % of Contracts
comb3_award_plot_binning <- interflex(Y = "award", D = "share_numcontracts_clique", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df[df$share_numcontracts_clique<0.0003851214,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection  (Alt. 3)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(comb3_award_plot_binning,theme.bw = TRUE)

## Panel (D) % of Contracts (Binary)
comb4_award_plot_binning <- interflex(Y = "award", D = "connectedbin_sharenumc", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df[which(df$b_capacity<.65,df$share_numcontracts_clique<0.0003851214),], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection  (Alt. 4)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(comb4_award_plot_binning,theme.bw = TRUE)

## Panel (E) % Value of Contracts
comb5_award_plot_binning <- interflex(Y = "award", D = "share_valcontracts_clique", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df[which(df$b_capacity<.65, df$share_valcontracts_clique<0.002284887),], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection (Alt. 5)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(comb5_award_plot_binning,theme.bw = TRUE)

## Panel (F) % Value of Contracts (Binary)
comb6_award_plot_binning <- interflex(Y = "award", D = "connectedbin_sharevalc", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df[which(df$b_capacity<.65, df$share_valcontracts_clique<0.002284887),], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection (Alt. 6)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(comb6_award_plot_binning,theme.bw = TRUE)


# Figure C6: Linear Interaction Diagnostics Plots - Procurement Contracts and Alternative Political Connections Measures

## Panel (A) Average Contract Size
comb1 <- interflex(estimator = "raw", Y = "award", D = "rent_usdk", X = "b_capacity", ncols=1, data = df, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection (Alt. 1)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(comb1,theme.bw = TRUE)

## Panel (B) Connection 75th
summary(df$rent_usdk)
# Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
# 0.000    1.625   11.800   38.811   55.673 1999.683 

df$connectedbin_75 <- NA
df$connectedbin_75[which(df$rent_usdk <= 55.673 )] <- 0
df$connectedbin_75[which(df$rent_usdk > 55.673 )] <- 1

comb2 <- interflex(estimator = "raw", Y = "award", D = "connectedbin_75", X = "b_capacity", ncols=2, data = df, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection (Alt. 2)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(comb2,theme.bw = TRUE)

## Panel (C) % of Contracts
comb3 <- interflex(estimator = "raw", Y = "award", D = "share_numcontracts_clique", X = "b_capacity", ncols=1, data = df, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection (Alt. 3)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(comb3,theme.bw = TRUE)

## Panel (D) % of Contracts (Binary)
comb4 <- interflex(estimator = "raw", Y = "award", D = "connectedbin_sharenumc", X = "b_capacity", ncols=2, data = df, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection (Alt. 4)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(comb4,theme.bw = TRUE)

## Panel (E) % Value of Contracts
comb5 <- interflex(estimator = "raw", Y = "award", D = "share_valcontracts_clique", X = "b_capacity", ncols=1, data = df, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection (Alt. 5)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(comb5,theme.bw = TRUE)

## Panel (F) % Value of Contracts (Binary)
comb6 <- interflex(estimator = "raw", Y = "award", D = "connectedbin_sharevalc", X = "b_capacity", ncols=2, data = df, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award", Dlabel = "Connection (Alt. 6)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(comb6,theme.bw = TRUE)



# Table C8: Political Connections, Bureaucrats’ Capabilities and Awards (Alternative Connections Measures)

## Linear Probability Models
## Model 1: Model with controls and FEs; connection as continuous variable based on average value extracted from clique
acon_lm1 <- lm(award ~ b_capacity + rent_usdk + b_capacity:rent_usdk + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[which(df$b_capacity<=0.6,df$rent_usdk<=103.8725),])
acon_lm1$vcov <- vcovCL(acon_lm1, cluster = ~ entity_code + year + nit_bidder)
acon_lm1_coef = coeftest(acon_lm1, acon_lm1$vcov)
acon_lm1_cse <- sqrt(diag(acon_lm1$vcov))

## Model 2: Model with controls and FEs; connection binary variable based on average value extracted from clique (1 if average value above the 75th percentile)
acon_lm2 <- lm(award ~ b_capacity + connectedbin_75 + b_capacity:connectedbin_75 + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[which(df$b_capacity<=0.6,df$rent_usdk<=103.8725),])
acon_lm2$vcov <- vcovCL(acon_lm2, cluster = ~ entity_code + year + nit_bidder)
acon_lm2_coef = coeftest(acon_lm2, acon_lm2$vcov)
acon_lm2_cse <- sqrt(diag(acon_lm2$vcov))

## Model 3: Model with controls and FEs; continuous measure connection based on share of contracts with clique
acon_lm3 <- lm(award ~ b_capacity + share_numcontracts_clique + b_capacity:share_numcontracts_clique + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[df$share_numcontracts_clique<0.0003851214,])
acon_lm3$vcov <- vcovCL(acon_lm3, cluster = ~ entity_code + year + nit_bidder)
acon_lm3_coef = coeftest(acon_lm3, acon_lm3$vcov)
acon_lm3_cse <- sqrt(diag(acon_lm3$vcov))

## Model 4: Model with controls and FEs; binary connection based on share of contracts with clique (1 if average share of contracts is above the mean)
acon_lm4 <- lm(award ~ b_capacity + connectedbin_sharenumc + b_capacity:connectedbin_sharenumc + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[which(df$b_capacity<.65,df$share_numcontracts_clique<0.0003851214),])
acon_lm4$vcov <- vcovCL(acon_lm4, cluster = ~ entity_code + year + nit_bidder)
acon_lm4_coef = coeftest(acon_lm4, acon_lm4$vcov)
acon_lm4_cse <- sqrt(diag(acon_lm4$vcov))

## Model 5: Model with controls and FEs; continuous measure of connection based on share of value contracts with clique 
acon_lm5 <- lm(award ~ b_capacity + share_valcontracts_clique + b_capacity:share_valcontracts_clique + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[which(df$b_capacity<.65, df$share_valcontracts_clique<0.002284887),])
acon_lm5$vcov <- vcovCL(acon_lm5, cluster = ~ entity_code + year + nit_bidder)
acon_lm5_coef = coeftest(acon_lm5, acon_lm5$vcov)
acon_lm5_cse <- sqrt(diag(acon_lm5$vcov))

## Model 6: Model with controls and FEs; binary measure of connection based on share of value contracts with clique (1 if the average share of the value of contracts is above the mean)
acon_lm6 <- lm(award ~ b_capacity + connectedbin_sharevalc + b_capacity:connectedbin_sharevalc + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[which(df$b_capacity<.65, df$share_valcontracts_clique<0.002284887),])
acon_lm6$vcov <- vcovCL(acon_lm6, cluster = ~ entity_code + year + nit_bidder)
acon_lm6_coef = coeftest(acon_lm6, acon_lm6$vcov)
acon_lm6_cse <- sqrt(diag(acon_lm6$vcov))


# TABLE with clustered standard errors:
stargazer(acon_lm1, acon_lm2, acon_lm3, acon_lm4, acon_lm5, acon_lm6, se = list(acon_lm1_cse, acon_lm2_cse, acon_lm3_cse, acon_lm4_cse, acon_lm5_cse, acon_lm6_cse), title="Political Connections, Bureaucrats' Capabilities and Awards (Alternative Connections Measures)",
          align=TRUE, dep.var.labels=c("award"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"), 
          no.space=TRUE, add.lines = list(c("Controls", "Y","Y","Y","Y","Y","Y"),
                                          c("Municipality FE", "Y","Y","Y","Y","Y","Y"),
                                          c("Year FE","Y","Y","Y","Y","Y","Y")),  keep = c("Constant","b_capacity", "rent_usdk","connectedbin_75","share_numcontracts_clique","connectedbin_sharenumc","share_valcontracts_clique","connectedbin_sharevalc"))




# Table C9: Political Connections, Bureaucrats’ Capabilities and Awards (Alternative Connections Measures, Logistic Regressions)

## Logistic Regressions
## Model 1: Model with controls and FEs; connection as continuous variable based on average value extracted from clique
acon_lgt1 <- glm(award ~ b_capacity + rent_usdk + b_capacity:rent_usdk + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[which(df$b_capacity<=0.6,df$rent_usdk<=103.8725),], family = binomial)
acon_lgt1$vcov <- vcovCL(acon_lgt1, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
acon_lgt1_coef = coeftest(acon_lgt1, acon_lgt1$vcov)
acon_lgt1_cse <- sqrt(diag(acon_lgt1$vcov))

## Model 2: Model with controls and FEs; connection binary variable based on average value extracted from clique (1 if average value above the 75th percentile)
acon_lgt2 <- glm(award ~ b_capacity + connectedbin_75 + b_capacity:connectedbin_75 + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[which(df$b_capacity<=0.6,df$rent_usdk<=103.8725),], family = binomial)
acon_lgt2$vcov <- vcovCL(acon_lgt2, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
acon_lgt2_coef = coeftest(acon_lgt2, acon_lgt2$vcov)
acon_lgt2_cse <- sqrt(diag(acon_lgt2$vcov))

## Model 3: Model with controls and FEs; continuous measure connection based on share of contracts with clique
acon_lgt3 <- glm(award ~ b_capacity + share_numcontracts_clique + b_capacity:share_numcontracts_clique + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[df$share_numcontracts_clique<0.0003851214,], family = binomial)
acon_lgt3$vcov <- vcovCL(acon_lgt3, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
acon_lgt3_coef = coeftest(acon_lgt3, acon_lgt3$vcov)
acon_lgt3_cse <- sqrt(diag(acon_lgt3$vcov))

## Model 4: Model with controls and FEs; binary connection based on share of contracts with clique (1 if average share of contracts is above the mean)
acon_lgt4 <- glm(award ~ b_capacity + connectedbin_sharenumc + b_capacity:connectedbin_sharenumc + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[which(df$b_capacity<.65,df$share_numcontracts_clique<0.0003851214),], family = binomial)
acon_lgt4$vcov <- vcovCL(acon_lgt4, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
acon_lgt4_coef = coeftest(acon_lgt4, acon_lgt4$vcov)
acon_lgt4_cse <- sqrt(diag(acon_lgt4$vcov))

## Model 5: Model with controls and FEs; continuous measure of connection based on share of value contracts with clique 
acon_lgt5 <- glm(award ~ b_capacity + share_valcontracts_clique + b_capacity:share_valcontracts_clique + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[which(df$b_capacity<.65, df$share_valcontracts_clique<0.002284887),], family = binomial)
acon_lgt5$vcov <- vcovCL(acon_lgt5, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
acon_lgt5_coef = coeftest(acon_lgt5, acon_lgt5$vcov)
acon_lgt5_cse <- sqrt(diag(acon_lgt5$vcov))

## Model 6: Model with controls and FEs; binary measure of connection based on share of value contracts with clique (1 if the average share of the value of contracts is above the mean)
acon_lgt6 <- glm(award ~ b_capacity + connectedbin_sharevalc + b_capacity:connectedbin_sharevalc + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df[which(df$b_capacity<.65, df$share_valcontracts_clique<0.002284887),], family = binomial)
acon_lgt6$vcov <- vcovCL(acon_lgt6, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
acon_lgt6_coef = coeftest(acon_lgt6, acon_lgt6$vcov)
acon_lgt6_cse <- sqrt(diag(acon_lgt6$vcov))


# TABLE with clustered standard errors:
stargazer(acon_lgt1, acon_lgt2, acon_lgt3, acon_lgt4, acon_lgt5, acon_lgt6, se = list(acon_lgt1_cse, acon_lgt2_cse, acon_lgt3_cse, acon_lgt4_cse, acon_lgt5_cse, acon_lgt6_cse), title="Connections, Bureaucrats' Capabilities and Awards (Alternative Connections Measures, Logistic Regressions)",
          align=TRUE, dep.var.labels=c("award"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"), 
          no.space=TRUE, add.lines = list(c("Controls", "Y","Y","Y","Y","Y","Y"),
                                          c("Municipality FE", "Y","Y","Y","Y","Y","Y"),
                                          c("Year FE","Y","Y","Y","Y","Y","Y")),  keep = c("Constant","b_capacity", "rent_usdk","connectedbin_75","share_numcontracts_clique","connectedbin_sharenumc","share_valcontracts_clique","connectedbin_sharevalc"))


# Figure C7: Alternative Political Connections, Bureaucrats’ Capabilities and Competition (Binning Estimator)

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Panel (A) Average Contract Size
rcomb1_award_plot_binning <- interflex(Y = "risky_award", D = "rent_usdk", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[which(df_nog$b_capacity<=0.6,df_nog$rent_usdk<=117.135),], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection (Alt. 1)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(rcomb1_award_plot_binning,theme.bw = TRUE)

## Panel (B) Connection 75th
summary(df_nog$rent_usdk)
# Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
# 0.000    2.193   18.897   45.423   66.094 1999.683 

df_nog$connectedbin_75 <- NA
df_nog$connectedbin_75[which(df_nog$rent_usdk <= 66.094 )] <- 0
df_nog$connectedbin_75[which(df_nog$rent_usdk > 66.094 )] <- 1

rcomb2_award_plot_binning <- interflex(Y = "risky_award", D = "connectedbin_75", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[df_nog$b_capacity<=0.6,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection (Alt. 2)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(rcomb2_award_plot_binning,theme.bw = TRUE)

## Panel (C) % of Contract
rcomb3_award_plot_binning <- interflex(Y = "risky_award", D = "share_numcontracts_clique", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[which(df_nog$b_capacity<0.65,df_nog$share_numcontracts_clique<0.0003713374),], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection  (Alt. 3)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(rcomb3_award_plot_binning,theme.bw = TRUE)

## Panel (D) % of Contracts (Binary)
rcomb4_award_plot_binning <- interflex(Y = "risky_award", D = "connectedbin_sharenumc", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[which(df_nog$b_capacity<.65,df_nog$share_numcontracts_clique<0.0003713374),], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection  (Alt. 4)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(rcomb4_award_plot_binning,theme.bw = TRUE)

## Panel (E) % of Value of Contracts
rcomb5_award_plot_binning <- interflex(Y = "risky_award", D = "share_valcontracts_clique", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[which(df_nog$b_capacity<.65, df_nog$share_valcontracts_clique<0.002647359),], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection (Alt. 5)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(rcomb5_award_plot_binning,theme.bw = TRUE)

## Panel (F) % of Value of Contracts (Binary)
rcomb6_award_plot_binning <- interflex(Y = "risky_award", D = "connectedbin_sharevalc", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[which(df_nog$b_capacity<.65, df_nog$share_valcontracts_clique<0.002647359),], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection (Alt. 6)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(rcomb6_award_plot_binning,theme.bw = TRUE)

## Panel (G) Average Contract Size
nbcomb1_award_plot_binning <- interflex(Y = "num_bids", D = "rent_usdk", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[which(df_nog$b_capacity<=0.6,df_nog$rent_usdk<=117.135),], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Number of Bids", Dlabel = "Connection (Alt. 1)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbcomb1_award_plot_binning,theme.bw = TRUE)

## Panel (H) Connection 75th
nbcomb2_award_plot_binning <- interflex(Y = "num_bids", D = "connectedbin_75", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[df_nog$b_capacity<=0.6,], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Number of Bids", Dlabel = "Connection (Alt. 2)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbcomb2_award_plot_binning,theme.bw = TRUE)

## Panel (I) % of Contract
nbcomb3_award_plot_binning <- interflex(Y = "num_bids", D = "share_numcontracts_clique", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[which(df_nog$b_capacity<0.65,df_nog$share_numcontracts_clique<0.0003713374),], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Number of Bids", Dlabel = "Connection  (Alt. 3)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbcomb3_award_plot_binning,theme.bw = TRUE)

## Panel (J) % of Contracts (Binary)
nbcomb4_award_plot_binning <- interflex(Y = "num_bids", D = "connectedbin_sharenumc", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[which(df_nog$b_capacity<.65,df_nog$share_numcontracts_clique<0.0003713374),], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Number of Bids", Dlabel = "Connection  (Alt. 4)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbcomb4_award_plot_binning,theme.bw = TRUE)

## Panel (K) % of Value of Contracts
nbcomb5_award_plot_binning <- interflex(Y = "num_bids", D = "share_valcontracts_clique", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[which(df_nog$b_capacity<.65, df_nog$share_valcontracts_clique<0.002647359),], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Number of Bids", Dlabel = "Connection (Alt. 5)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbcomb5_award_plot_binning,theme.bw = TRUE)

## Panel (L) % of Value of Contracts (Binary)
nbcomb6_award_plot_binning <- interflex(Y = "num_bids", D = "connectedbin_sharevalc", X = "b_capacity", Z=c("construction", "logvalcontracts", "percontracts_muninei", "percontracts_munisam", "modality_cat", "numcontracts_gov", "firm_procage", "iic", "logpopulation", "party_president"),  data = df_nog[which(df_nog$b_capacity<.65, df_nog$share_valcontracts_clique<0.002647359),], estimator = "binning", FE = c("entity_code", "year"), vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Number of Bids", Dlabel = "Connection (Alt. 6)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbcomb6_award_plot_binning,theme.bw = TRUE)
#ggsave(paste0(wd_save, "bn_effects_award_nbcomb6.pdf"), plot = nbcomb6_award_plot_binning, width = 10, height = 6)



# Figure C8: Linear Interaction Diagnostics Plots - Competition and Alternative Political Connections Measures

## Subset of bids that won contracts
df_nog <- df[ which(df$award==1), ]

## Panel (A) Average Contract Size
rcomb1 <- interflex(estimator = "raw", Y = "risky_award", D = "rent_usdk", X = "b_capacity", ncols=1, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection (Alt. 1)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(rcomb1,theme.bw = TRUE)

## Panel (B) Connection 75th
summary(df_nog$rent_usdk)
# Min.  1st Qu.   Median     Mean  3rd Qu.     Max. 
# 0.000    2.193   18.897   45.423   66.094 1999.683 

df_nog$connectedbin_75 <- NA
df_nog$connectedbin_75[which(df_nog$rent_usdk <= 66.094 )] <- 0
df_nog$connectedbin_75[which(df_nog$rent_usdk > 66.094 )] <- 1

rcomb2 <- interflex(estimator = "raw", Y = "risky_award", D = "connectedbin_75", X = "b_capacity", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection (Alt. 2)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(rcomb2,theme.bw = TRUE)

## Panel (C) % of Contract
rcomb3 <- interflex(estimator = "raw", Y = "risky_award", D = "share_numcontracts_clique", X = "b_capacity", ncols=1, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection (Alt. 3)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(rcomb3,theme.bw = TRUE)

## Panel (D) % of Contracts (Binary)
rcomb4 <- interflex(estimator = "raw", Y = "risky_award", D = "connectedbin_sharenumc", X = "b_capacity", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection (Alt. 4)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(rcomb4,theme.bw = TRUE)

## Panel (E) % of Value of Contracts
rcomb5 <- interflex(estimator = "raw", Y = "risky_award", D = "share_valcontracts_clique", X = "b_capacity", ncols=1, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection (Alt. 5)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(rcomb5,theme.bw = TRUE)

## Panel (F) % of Value of Contracts (Binary)
rcomb6 <- interflex(estimator = "raw", Y = "risky_award", D = "connectedbin_sharevalc", X = "b_capacity", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Award to Single Bidder", Dlabel = "Connection (Alt. 6)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(rcomb6,theme.bw = TRUE)

## Panel (G) Average Contract Size
nbcomb1 <- interflex(estimator = "raw", Y = "num_bids", D = "rent_usdk", X = "b_capacity", ncols=1, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Number of Bids", Dlabel = "Connection (Alt. 1)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbcomb1,theme.bw = TRUE)

## Panel (H) Connection 75th
nbcomb2 <- interflex(estimator = "raw", Y = "num_bids", D = "connectedbin_75", X = "b_capacity", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Number of Bids", Dlabel = "Connection (Alt. 2)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbcomb2,theme.bw = TRUE)

## Panel (I) % of Contract
nbcomb3 <- interflex(estimator = "raw", Y = "num_bids", D = "share_numcontracts_clique", X = "b_capacity", ncols=1, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Number of Bids", Dlabel = "Connection (Alt. 3)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbcomb3,theme.bw = TRUE)

## Panel (J) % of Contracts (Binary)
nbcomb4 <- interflex(estimator = "raw", Y = "num_bids", D = "connectedbin_sharenumc", X = "b_capacity", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Number of Bids", Dlabel = "Connection (Alt. 4)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbcomb4,theme.bw = TRUE)

## Panel (K) % of Value of Contracts
nbcomb5 <- interflex(estimator = "raw", Y = "num_bids", D = "share_valcontracts_clique", X = "b_capacity", ncols=1, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Number of Bids", Dlabel = "Connection (Alt. 5)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbcomb5,theme.bw = TRUE)

## Panel (L) % of Value of Contracts (Binary)
nbcomb6 <- interflex(estimator = "raw", Y = "num_bids", D = "connectedbin_sharevalc", X = "b_capacity", ncols=2, data = df_nog, vcov.type = "cluster", cl = c("entity_code","year","nit_bidder"), main = "Marginal Effects", ylim = c(-15, 15), Ylabel = "Number of Bids", Dlabel = "Connection (Alt. 6)", Xlabel="Bureaucrat's Capabilities", theme.bw = TRUE)
plot(nbcomb6,theme.bw = TRUE)



# Table C10: Political Connections, Bureaucrats’ Capabilities and Awards as Single Bidders (Alternative Connections Measures)

## Linear Probability Models
## Model 1: Model with controls and FEs; connection as continuous variable based on average value extracted from clique
racon_lm1 <- lm(risky_award ~ b_capacity + rent_usdk + b_capacity:rent_usdk + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<=0.6,df_nog$rent_usdk<=117.135),])
racon_lm1$vcov <- vcovCL(racon_lm1, cluster = ~ entity_code + year + nit_bidder)
racon_lm1_coef = coeftest(racon_lm1, racon_lm1$vcov)
racon_lm1_cse <- sqrt(diag(racon_lm1$vcov))

## Model 2: Model with controls and FEs; connection binary variable based on average value extracted from clique (1 if average value above the 75th percentile)
racon_lm2 <- lm(risky_award ~ b_capacity + connectedbin_75 + b_capacity:connectedbin_75 + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<=0.6,df_nog$rent_usdk<=117.135),])
racon_lm2$vcov <- vcovCL(racon_lm2, cluster = ~ entity_code + year + nit_bidder)
racon_lm2_coef = coeftest(racon_lm2, racon_lm2$vcov)
racon_lm2_cse <- sqrt(diag(racon_lm2$vcov))

## Model 3: Model with controls and FEs; continuous measure connection based on share of contracts with clique
racon_lm3 <- lm(risky_award ~ b_capacity + share_numcontracts_clique + b_capacity:share_numcontracts_clique + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<0.65,df_nog$share_numcontracts_clique<0.0003713374,),])
racon_lm3$vcov <- vcovCL(racon_lm3, cluster = ~ entity_code + year + nit_bidder)
racon_lm3_coef = coeftest(racon_lm3, racon_lm3$vcov)
racon_lm3_cse <- sqrt(diag(racon_lm3$vcov))

## Model 4: Model with controls and FEs; binary connection based on share of contracts with clique (1 if average share of contracts is above the mean)
racon_lm4 <- lm(risky_award ~ b_capacity + connectedbin_sharenumc + b_capacity:connectedbin_sharenumc + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<.65,df_nog$share_numcontracts_clique<0.0003713374),])
racon_lm4$vcov <- vcovCL(racon_lm4, cluster = ~ entity_code + year + nit_bidder)
racon_lm4_coef = coeftest(racon_lm4, racon_lm4$vcov)
racon_lm4_cse <- sqrt(diag(racon_lm4$vcov))

## Model 5: Model with controls and FEs; continuous measure of connection based on share of value contracts with clique 
racon_lm5 <- lm(risky_award ~ b_capacity + share_valcontracts_clique + b_capacity:share_valcontracts_clique + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<.65, df_nog$share_valcontracts_clique<0.002647359),])
racon_lm5$vcov <- vcovCL(racon_lm5, cluster = ~ entity_code + year + nit_bidder)
racon_lm5_coef = coeftest(racon_lm5, racon_lm5$vcov)
racon_lm5_cse <- sqrt(diag(racon_lm5$vcov))

## Model 6: Model with controls and FEs; binary measure of connection based on share of value contracts with clique (1 if the average share of the value of contracts is above the mean)
racon_lm6 <- lm(risky_award ~ b_capacity + connectedbin_sharevalc + b_capacity:connectedbin_sharevalc + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<.65, df_nog$share_valcontracts_clique<0.002647359),])
racon_lm6$vcov <- vcovCL(racon_lm6, cluster = ~ entity_code + year + nit_bidder)
racon_lm6_coef = coeftest(racon_lm6, racon_lm6$vcov)
racon_lm6_cse <- sqrt(diag(racon_lm6$vcov))


# TABLE with clustered standard errors:
stargazer(racon_lm1, racon_lm2, racon_lm3, racon_lm4, racon_lm5, racon_lm6, se = list(racon_lm1_cse, racon_lm2_cse, racon_lm3_cse, racon_lm4_cse, racon_lm5_cse, racon_lm6_cse), title="Connections, Bureaucrats' Capabilities and Awards as Single Bidders (Alternative Connections Measures)",
          align=TRUE, dep.var.labels=c("risky_award"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"), 
          no.space=TRUE, add.lines = list(c("Controls", "Y","Y","Y","Y","Y","Y"),
                                          c("Municipality FE", "Y","Y","Y","Y","Y","Y"),
                                          c("Year FE","Y","Y","Y","Y","Y","Y")),  keep = c("Constant","b_capacity", "rent_usdk","connectedbin_75","share_numcontracts_clique","connectedbin_sharenumc","share_valcontracts_clique","connectedbin_sharevalc"))


# Table C11: Political Connections, Bureaucrats’ Capabilities and Awards as Single Bidders (Alternative Connections Measures, Logistic Regressions)

## Logistic Regressions
## Model 1: Model with controls and FEs; connection as continuous variable based on average value extracted from clique
racon_lgt1 <- glm(risky_award ~ b_capacity + rent_usdk + b_capacity:rent_usdk + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<=0.6,df_nog$rent_usdk<=117.135),], family = binomial)
racon_lgt1$vcov <- vcovCL(racon_lgt1, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
racon_lgt1_coef = coeftest(racon_lgt1, racon_lgt1$vcov)
racon_lgt1_cse <- sqrt(diag(racon_lgt1$vcov))

## Model 2: Model with controls and FEs; connection binary variable based on average value extracted from clique (1 if average value above the 75th percentile)
racon_lgt2 <- glm(risky_award ~ b_capacity + connectedbin_75 + b_capacity:connectedbin_75 + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<=0.6,df_nog$rent_usdk<=117.135),], family = binomial)
racon_lgt2$vcov <- vcovCL(racon_lgt2, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
racon_lgt2_coef = coeftest(racon_lgt2, racon_lgt2$vcov)
racon_lgt2_cse <- sqrt(diag(racon_lgt2$vcov))

## Model 3: Model with controls and FEs; continuous measure connection based on share of contracts with clique
racon_lgt3 <- glm(risky_award ~ b_capacity + share_numcontracts_clique + b_capacity:share_numcontracts_clique + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<0.65,df_nog$share_numcontracts_clique<0.0003713374),], family = binomial)
racon_lgt3$vcov <- vcovCL(racon_lgt3, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
racon_lgt3_coef = coeftest(racon_lgt3, racon_lgt3$vcov)
racon_lgt3_cse <- sqrt(diag(racon_lgt3$vcov))

## Model 4: Model with controls and FEs; binary connection based on share of contracts with clique (1 if average share of contracts is above the mean)
racon_lgt4 <- glm(risky_award ~ b_capacity + connectedbin_sharenumc + b_capacity:connectedbin_sharenumc + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<.65,df_nog$share_numcontracts_clique<0.0003713374),], family = binomial)
racon_lgt4$vcov <- vcovCL(racon_lgt4, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
racon_lgt4_coef = coeftest(racon_lgt4, racon_lgt4$vcov)
racon_lgt4_cse <- sqrt(diag(racon_lgt4$vcov))

## Model 5: Model with controls and FEs; continuous measure of connection based on share of value contracts with clique 
racon_lgt5 <- glm(risky_award ~ b_capacity + share_valcontracts_clique + b_capacity:share_valcontracts_clique + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<.65, df_nog$share_valcontracts_clique<0.002647359),], family = binomial)
racon_lgt5$vcov <- vcovCL(racon_lgt5, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
racon_lgt5_coef = coeftest(racon_lgt5, racon_lgt5$vcov)
racon_lgt5_cse <- sqrt(diag(racon_lgt5$vcov))

## Model 6: Model with controls and FEs; binary measure of connection based on share of value contracts with clique (1 if the average share of the value of contracts is above the mean)
racon_lgt6 <- glm(risky_award ~ b_capacity + connectedbin_sharevalc + b_capacity:connectedbin_sharevalc + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<.65, df_nog$share_valcontracts_clique<0.002647359),], family = binomial)
racon_lgt6$vcov <- vcovCL(racon_lgt6, cluster = ~ entity_code + year + nit_bidder, type = "HC0")
racon_lgt6_coef = coeftest(racon_lgt6, racon_lgt6$vcov)
racon_lgt6_cse <- sqrt(diag(racon_lgt6$vcov))


# TABLE with clustered standard errors:
stargazer(racon_lgt1, racon_lgt2, racon_lgt3, racon_lgt4, racon_lgt5, racon_lgt6, se = list(racon_lgt1_cse, racon_lgt2_cse, racon_lgt3_cse, racon_lgt4_cse, racon_lgt5_cse, racon_lgt6_cse), title="Connections, Bureaucrats' Capabilities and Awards as Single Bidders (Alternative Connections Measures, Logistic Regressions)",
          align=TRUE, dep.var.labels=c("risky_award"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"), 
          no.space=TRUE, add.lines = list(c("Controls", "Y","Y","Y","Y","Y","Y"),
                                          c("Municipality FE", "Y","Y","Y","Y","Y","Y"),
                                          c("Year FE","Y","Y","Y","Y","Y","Y")),  keep = c("Constant","b_capacity", "rent_usdk","connectedbin_75","share_numcontracts_clique","connectedbin_sharenumc","share_valcontracts_clique","connectedbin_sharevalc"))





# Table C12: Political Connections, Bureaucrats’ Capabilities and the Number of Competing Bids (Alternative Connections Measures)

## Model 1: Model with controls and FEs; connection as continuous variable based on average value extracted from clique
nbacon_lm1 <- lm(num_bids ~ b_capacity + rent_usdk + b_capacity:rent_usdk + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<=0.6,df_nog$rent_usdk<=117.135),])
nbacon_lm1$vcov <- vcovCL(nbacon_lm1, cluster = ~ entity_code + year + nit_bidder)
nbacon_lm1_coef = coeftest(nbacon_lm1, nbacon_lm1$vcov)
nbacon_lm1_cse <- sqrt(diag(nbacon_lm1$vcov))

## Model 2: Model with controls and FEs; connection binary variable based on average value extracted from clique (1 if average value above the 75th percentile)
nbacon_lm2 <- lm(num_bids ~ b_capacity + connectedbin_75 + b_capacity:connectedbin_75 + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<=0.6,df_nog$rent_usdk<=117.135),])
nbacon_lm2$vcov <- vcovCL(nbacon_lm2, cluster = ~ entity_code + year + nit_bidder)
nbacon_lm2_coef = coeftest(nbacon_lm2, nbacon_lm2$vcov)
nbacon_lm2_cse <- sqrt(diag(nbacon_lm2$vcov))

## Model 3: Model with controls and FEs; continuous measure connection based on share of contracts with clique
nbacon_lm3 <- lm(num_bids ~ b_capacity + share_numcontracts_clique + b_capacity:share_numcontracts_clique + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<0.65,df_nog$share_numcontracts_clique<0.0003713374,),])
nbacon_lm3$vcov <- vcovCL(nbacon_lm3, cluster = ~ entity_code + year + nit_bidder)
nbacon_lm3_coef = coeftest(nbacon_lm3, nbacon_lm3$vcov)
nbacon_lm3_cse <- sqrt(diag(nbacon_lm3$vcov))

## Model 4: Model with controls and FEs; binary connection based on share of contracts with clique (1 if average share of contracts is above the mean)
nbacon_lm4 <- lm(num_bids ~ b_capacity + connectedbin_sharenumc + b_capacity:connectedbin_sharenumc + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<.65,df_nog$share_numcontracts_clique<0.0003713374),])
nbacon_lm4$vcov <- vcovCL(nbacon_lm4, cluster = ~ entity_code + year + nit_bidder)
nbacon_lm4_coef = coeftest(nbacon_lm4, nbacon_lm4$vcov)
nbacon_lm4_cse <- sqrt(diag(nbacon_lm4$vcov))

## Model 5: Model with controls and FEs; continuous measure of connection based on share of value contracts with clique 
nbacon_lm5 <- lm(num_bids ~ b_capacity + share_valcontracts_clique + b_capacity:share_valcontracts_clique + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<.65, df_nog$share_valcontracts_clique<0.002647359),])
nbacon_lm5$vcov <- vcovCL(nbacon_lm5, cluster = ~ entity_code + year + nit_bidder)
nbacon_lm5_coef = coeftest(nbacon_lm5, nbacon_lm5$vcov)
nbacon_lm5_cse <- sqrt(diag(nbacon_lm5$vcov))

## Model 6: Model with controls and FEs; binary measure of connection based on share of value contracts with clique (1 if the average share of the value of contracts is above the mean)
nbacon_lm6 <- lm(num_bids ~ b_capacity + connectedbin_sharevalc + b_capacity:connectedbin_sharevalc + construction + logvalcontracts + percontracts_muninei + percontracts_munisam + factor(modality_cat) + numcontracts_gov + firm_procage + iic + logpopulation + party_president + factor(entity_code) + factor(year), data = df_nog[which(df_nog$b_capacity<.65, df_nog$share_valcontracts_clique<0.002647359),])
nbacon_lm6$vcov <- vcovCL(nbacon_lm6, cluster = ~ entity_code + year + nit_bidder)
nbacon_lm6_coef = coeftest(nbacon_lm6, nbacon_lm6$vcov)
nbacon_lm6_cse <- sqrt(diag(nbacon_lm6$vcov))


# TABLE with clustered standard errors:
stargazer(nbacon_lm1, nbacon_lm2, nbacon_lm3, nbacon_lm4, nbacon_lm5, nbacon_lm6, se = list(nbacon_lm1_cse, nbacon_lm2_cse, nbacon_lm3_cse, nbacon_lm4_cse, nbacon_lm5_cse, nbacon_lm6_cse), title="Connections, Bureucrats' Capabilities and the Number of Competing Bids (Alternative Connections Measures)",
          align=TRUE, dep.var.labels=c("num_bids"), star.cutoffs = c(0.1, 0.05, 0.01, 0.001), star.char = c("+", "*", "**","***"), 
          no.space=TRUE, add.lines = list(c("Controls", "Y","Y","Y","Y","Y","Y"),
                                          c("Municipality FE", "Y","Y","Y","Y","Y","Y"),
                                          c("Year FE","Y","Y","Y","Y","Y","Y")),  keep = c("Constant","b_capacity", "rent_usdk","connectedbin_75","share_numcontracts_clique","connectedbin_sharenumc","share_valcontracts_clique","connectedbin_sharevalc"))

